1. 程式人生 > >自己製作軟盤映象檔案

自己製作軟盤映象檔案

2006-10-07

現在較以往編寫作業系統方便的地方就是有許多虛擬機器可以應用,因此不必要在硬體上不停重啟機器。然而,新機器很多也沒有軟碟機。因此需要用模擬的軟碟機,即用一個img檔案來代替軟碟機。vmware和bochs都支援模擬軟碟機。

但是img檔案如何生成呢?

1.使用winimage軟體。該軟體可以生成各種映象檔案。然而不可以直接寫二進位制,即未格式化的二進位制img檔案。

2.自己寫一個可以拷貝二進位制進而生成img檔案的程式碼(在linux AS4上編譯除錯通過):

/********************** 檔名 : writeimg.c 說 明 :將二進位制檔案寫成一個軟盤image檔案,用於引導

版權所有 2006, 周海漢,保留所有權利 Copyright 2006, ablo zhou. All Right Reserved.

#include /* unistd.h 需要這個檔案 / #include / 包含有read和write函式 / #include #include void menu(void) { printf(“write binary file to image file or floppyn copy right 2006 zhouhh nn using : n writeimg [-i infile] [-o outfile] n -i : binary input file n -o : output image file n eg:n writeimg -i boot.bin -o boot.imgn writeimg -i boot.bin -o /dev/fd0 n”); } int main(int argc,char** argv) { char in_file[256]=”boot.bin”; char out_file[256]=”BOOT.IMG”; int i=0; char boot_buf[1440

1024]={0}; int size=0; int floppy_desc, file_desc; if(argc == 1) { menu(); return; } for(i = 1; i < argc; i+=2) { if(i+1 > argc) { menu(); return; } if(strcmp(argv[i],”-i”)==0) { strcpy(in_file,argv[i+1]); } else if(strcmp(argv[i],”-o”)==0) { strcpy(out_file,argv[i+1]); } }

file_desc = open(in_file,O_RDONLY); if(file_desc <=0) { printf(“error: can’t open file %s n”,in_file); menu(); return; }

size = lseek(file_desc,0L,SEEK_END); lseek(file_desc,0L,SEEK_SET); read(file_desc, boot_buf, size); close(file_desc); boot_buf[510] = 0x55; boot_buf[511] = 0xaa;

floppy_desc = open(out_file,O_RDWR|O_CREAT); if(floppy_desc <=0) { printf(“error: can’t open file %s n”,out_file); menu(); return; } lseek(floppy_desc, 0, SEEK_CUR); write(floppy_desc, boot_buf, 1440*1024); close(floppy_desc); printf(“image file %s create successfully.n”,out_file); }

該程式可以直接拷貝二進位制到img檔案或者軟碟機。可以用於製作直接啟動盤。

如非註明轉載, 均為原創. 本站遵循知識共享CC協議,轉載請註明來源