科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件Windows下将linux启动代码写入磁盘

Windows下将linux启动代码写入磁盘

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

为配合Windows下的linux启动代码的编译运行,这里的C语言程序就是是把linux启动代码写入软盘并运行的配套程序。

作者:浪人BLUE 来源:CSDN 2008年3月27日

关键字: 代码 Linux Windows 开源

  • 评论
  • 分享微博
  • 分享邮件

1。write.c。此文件利用biosdisk可以写入软盘的任何指定扇区

#include<stdio.h>

#define SEC_MAX 1024
#define RETRY_MAX 5
#define TRACK_NUM 80  /*track number of a floppy disk*/
#define TR_SEC_NUM 18 /*sector number per track per head*/

main()
{
 unsigned char buffer[512];
 FILE *pFile=NULL;
 int nReturn,nSec,nRetry;  /*nSec is aready write sector number*/
 unsigned char nSread,nTrack,nHead; /*where to write or read from floppy disk*/
 
 if((pFile=fopen("sec3.exe","rb"))==NULL)
 {
  printf("\nfopen fail!");
  return;
 }
 
 nSec=0;
 nSread=6;
 nTrack=0;
 nHead=0;
 while(nSec<SEC_MAX)
 {
    /*read sector data from file and if read error retry and then return*/
   memset(buffer,0,512);
   nRetry=0;
   nReturn=0;
   while(nReturn!=512)
   {
      fseek(pFile,0x200+512L*nSec,0); /*seek to where to read,there must be set long*/
     nReturn=fread(buffer,1,512,pFile);
     if(nRetry>RETRY_MAX) /*if error then return*/
     {
      if(nReturn>1)
      {
       ;printf("\nRead file Error.Mybe get the end of file(sec=%d,nReturn=%x,nTrack=%x,nHead=%x,nSread=%x) !",nSec,nReturn,nTrack,nHead,nSread);
       printf("\nRead file Error.Mybe get the end of file(sec=%d,nReturn=%x) !",nSec,nReturn);
       nSec=SEC_MAX;  /*get end of the file,so after write to disk,will return */
       break;
      }
      else
      {
       printf("\nRead file Error.Mybe read error(sec=%d,nReturn=%x)  !",nSec,nReturn);
       fclose(pFile);
       return;
      }
     }
     nRetry++;
   }
   /*write data from buffer and if write error retry and then return*/
   nRetry=0;
   nReturn=1;
   while(nReturn!=0)
   {
       nReturn=biosdisk(3, /*cmd=3,write*/
                        0, /*driver=0,A flopy disk*/
                        nHead, /*head*/
                        nTrack, /*track*/
                        nSread, /*setor*/
                        1,     
                        buffer);
      if(nRetry>RETRY_MAX) /*if error then return*/
     {
      printf("\nbiosdisk return:%d,head(%d),track(%d),sector(%d)",nReturn,nHead,nTrack,nSread);
      fclose(pFile);
      return;
     }
     nRetry++;
    }
    /*adjust the parameters*/
    nSec++; /**/
      if(++nSread>18)
    {
          nSread=1;
      if(nHead==0) /*read current track on another head*/
      {
       nHead=1;
      }
      else         /*nHead==1,read next track*/
      {
       nHead=0;
        nTrack++;
      }
   }
 }
 fclose(pFile);
}

2。read.c.此文件利用biosdisk可以读软盘的任何指定扇区。

#include<stdio.h>

#define SEC_MAX 41
#define RETRY_MAX 5
#define TRACK_NUM 80  /*track number of a floppy disk*/
#define TR_SEC_NUM 18 /*sector number per track per head*/

main()
{
 unsigned char buffer[512];
 FILE *pFile=NULL;
 int nReturn,nSec,nRetry;  /*nSec is aready write sector number*/
 unsigned char nSread,nTrack,nHead; /*where to write or read from floppy disk*/
 
 if((pFile=fopen("sec3.bin","wb"))==NULL)
 {
  printf("\nfopen fail!");
  return;
 }
 
 /*seek to head and write 0x200*/
 fseek(pFile,0,0); 
 memset(buffer,0,512);
 fwrite(buffer,1,512,pFile);
 
 nSec=0;
 nSread=6;
 nTrack=0;
 nHead=0;
 while(nSec<SEC_MAX)
 {
   /*read data from disk and if write error retry and then return*/
   memset(buffer,0,512);
   nRetry=0;
   nReturn=1;
   while(nReturn!=0)
   {
       nReturn=biosdisk(2, /*cmd=3,read*/
                        0, /*driver=0,A flopy disk*/
                        nHead, /*head*/
                        nTrack, /*track*/
                        nSread, /*setor*/
                        1,     
                        buffer);
      if(nRetry>RETRY_MAX) /*if error then return*/
     {
      printf("\nbiosdisk return:%d,head(%d),track(%d),sector(%d)",nReturn,nHead,nTrack,nSread);
      fclose(pFile);
      return;
     }
     nRetry++;
    }
    /*read sector data from file and if read error retry and then return*/
   nRetry=0;
   nReturn=0;
   while(nReturn!=512)
   {
      fseek(pFile,0x200+512*nSec,0); /*seek to where to read*/
     nReturn=fwrite(buffer,1,512,pFile);
     if(nRetry>RETRY_MAX) /*if error then return*/
     {
      if(nReturn>1)
      {
       printf("\nRead file Error.Mybe get the end of file(sec=%d,nReturn=%x,nTrack=%x,nHead=%x,nSread=%x) !",nSec,nReturn,nTrack,nHead,nSread);
       nSec=SEC_MAX;  /*get end of the file,so after write to disk,will return */
       break;
      }
      else
      {
       printf("\nRead file Error.Mybe read error(sec=%d,nReturn=%x)  !",nSec,nReturn);
       fclose(pFile);
       return;
      }
     }
     nRetry++;
   }
    /*adjust the parameters*/
    nSec++; /**/
      if(++nSread>18)
    {
          nSread=1;
      if(nHead==0) /*read current track on another head*/
      {
       nHead=1;
      }
      else         /*nHead==1,read next track*/
      {
       nHead=0;
        nTrack++;
      }
   }
 }
 fclose(pFile);
}
3。merge.c.此文件可以合并exe文件。编写原因是:如果想把head.asm中的main函数分出来成为一个单独的文件,那么我想把这个分出来的文件编译连接后于head.exe和为一体写入软盘,就需要这个程序将二者合并、写入软盘。

#include <stdio.h>
#include <stdlib.h>

#define RETRY_MAX 5
#define SEC_MAX 1024

main()
{
 unsigned char buffer[512];
 FILE *pFile3=NULL,*pFileM=NULL,*pFileB=NULL;
 int nReturn,nRetry,nSec,nByte;
 unsigned char bRun;
 
 /*+++++++++++ first,copy code from sec3.exe to sec3.bin ++++++++++++*/
 /*=======open two file-sec3.exe and sec3.bin=======*/
 if((pFile3=fopen("sec3.exe","rb"))==NULL)
 {
  printf("\nfopen fail---file:sec3.exe!");
  return;
 }

  if((pFileB=fopen("sec3.bin","wb+"))==NULL)
 {
  printf("\nfopen fail***file:sec3.bin!");
  fclose(pFile3);
  return;
 }

 /*=======copy code from sec3.exe to sec3.bin========*/
 nSec=0;
 bRun=1;  /*bRun control the read loop*/
 while(bRun)
 {
    /*read 512 data from file and if read error retry and then return*/
   memset(buffer,0,512);
   nRetry=0;
   nReturn=0;
   while(nReturn!=512)
   {
      fseek(pFile3,0x0000+512L*nSec,0); /*seek to where to read,and there 512L must be long*/
      nReturn=fread(buffer,1,512,pFile3);
     if(nRetry>RETRY_MAX) /*if error then return*/
     {
       printf("\nRead file(sec3.exe).Get the end of file(sec=%d,nReturn=%x)!",nSec,nReturn);
       bRun=0;  /*get end of the file,so after write to file sec3.exe,will return */
       break;
     }
     nRetry++;
   }
   nByte=nReturn;

   /*write 512 data to file and if write error retry and then return*/
   nRetry=0;
   nReturn=0;
   while(nReturn!=nByte)
   {
       fseek(pFileB,0x0000+512L*nSec,0);  /*seek to where to write,and there 512L is long*/
     nReturn=fwrite(buffer,1,nByte,pFileB);   /*will write 512B or mor less*/
     if(nRetry>RETRY_MAX) /*if error then return*/
     {
      if(nReturn>1)
      {
       printf("\n**Write file Error.Mybe get the end of file(sec=%d,nReturn=%x)!",nSec,nReturn);
       break;
      }
      else
      {
       printf("\n**File write error(sec=%d,nReturn=%x)!",nSec,nReturn);
       fclose(pFile3);
       fclose(pFileB);
       return;
      }
     }
     nRetry++;
   }
    nSec++;
 }
 /*close sec3.exe file*/
 fclose(pFile3);

 /*+++++++++++ secode,merge code from main.exe to sec3.bin ++++++++++++*/
 /*=======open file-main.exe and sec3.bin=======*/
  if((pFileM=fopen("main.exe","rb"))==NULL)
 {
  printf("\nfopen fail++++file:main.exe!");
  fclose(pFileB);
  return;
 }
 
 /*=======copy code from main.exe to sec3.bin========*/
  nSec=0; 
  bRun=1;  /*bRun control the read loop*/
 while(bRun)
 {
    /*read 512 data from file and if read error retry and then return*/
   memset(buffer,0,512);
   nRetry=0;
   nReturn=0;
   while(nReturn!=512)
   {
      fseek(pFileM,0x7200+512L*nSec,0); /*seek to where to read*/
      nReturn=fread(buffer,1,512,pFileM);
     if(nRetry>RETRY_MAX) /*if error then return*/
     {
       printf("\nRead file(main.exe).Get the end of file(sec=%d,nReturn=%x)!",nSec,nReturn);
       bRun=0;  /*get end of the file,so after write to file sec3.exe,will break while */
       break;
     }
     nRetry++;
   }
   nByte=nReturn;
   
   /*write 512 data to file and if write error retry and then return*/
   nRetry=0;
   nReturn=0;
   while(nReturn!=nByte)
   {
          fseek(pFileB,0x7200+512L*nSec,0);  /*seek to where to write*/
     nReturn=fwrite(buffer,1,nByte,pFileB);   /*will write 512B or mor less*/
     if(nRetry>RETRY_MAX) /*if error then return*/
     {
      if(nReturn>1)
      {
              printf("\n**Write file Error.Mybe get the end of file(sec=%d,nReturn=%x)!\n",nSec,nReturn);
       break;
      }
      else
      {
       printf("\n**File write error(sec=%d,nReturn=%x)!",nSec,nReturn);
       fclose(pFileB);
       fclose(pFileM);
       return;
      }
     }
     nRetry++;
   }
    nSec++;
 }
 /*close main.exe file*/
 fclose(pFileM);

  /*delete and rename*/
  fclose(pFileB);

  system("del SEC3.EXE");
  system("rename sec3.bin sec3.exe");
  system("write3");

  return;

}

    好了,到此为止,linux的启动汇编代码就可以在windows下用tasm+tlink编译连接,用写入程序写入软盘来运行了。

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章