After reading "write an operating system by yourself", I also want to create a mini operating system myself. However, the floppy disk used in the book is almost extinct, and the USB flash disk is a favorite tool. Therefore, if you want to write the operating system into the USB flash drive, the key to success is to read and write the Boot Sector of the USB flash drive.
Format the USB flash drive as a FAT32 file system. To read and write the Boot Sector of the USB flash drive, you must first understand the FAT32 format. It consists of four parts:
Reserved sector | fat1 | fat2 | data zone. Note that the first sector of the reserved sector is the Boot Sector (DBR ).
The data structure of the boot sector is as follows:
The data area starts with the main directory area. The main directory area contains familiarity with files. The data structure is as follows:
Based on the cluster number at the beginning of each file, you can find the starting position of the absolute sector of the file content.
The file cluster number is stored in the form of a linked list installed in the fat table, and the file attributes are stored in the form of an array installed in the main directory.
Absolute start position of the file content = reserved sector + fat1 + fat2 + (Starting cluster number-2) * number of sectors per cluster
Then write the boot program, read the Boot Sector of the USB flash drive, and choose to start from the USB flash drive. Then you can see your own USB flash drive operating system.
The assembly code of the pilot program is as follows:
Org 07c00h <br/> JMP short label_start <br/> NOP </P> <p> OEM dB 'delete '; // 8 bytes <br/> bytes_per_sector DW 512 <br/> sectors_per_cluster dB 8 <br/> reserved_sectors DW 584 <br/> number_of_fats DB 2 <br/> root_entries DW 0 <br/> sectors_small DW 0 <br/> media_descriptor dB 0f8h <br/> export DW 0 <br/> sectors_per_track DW 63 <br/> heads DW 255 <br/> hidden_sectors dd 63 <br/> sectors dd 3902913 </ P> <p> sectors_per_fat dd 3804 <br/> extended_flags DW 0 <br/> Version DW 0 <br/> root_dir_1st_cluster DD 2 <br/> fsinfo_sector DW 1 <br/> backup_boot_sector DW 6 <br/> times 12 db 0 </P> <p> bios_drive dB 80 h <br/> db 0 <br/> ext_boot_signature dB 29 H <br/> volume_serial_number dd 15329558 <br/> volume_label dB 'let09 '; // 11 bytes <br/> file_system dB 'fat32'; // 8 bytes </P> <p> label_start: <br/> mov ax, CS <br/> MoV ds, ax <br/> mov es, ax <br/> call dispstr <br/> JMP $ </P> <p> dispstr: <br/> mov ax, bootmessage <br/> mov bp, ax <br/> mov CX, 15 <br/> mov ax, 01301 H <br/> mov BX, 000ch <br/> mov DL, 0 <br/> int 10 h </P> <p> bootmessage: DB 'hello, Kitty! ', 13, 10,' $ '<br/> times 510-($-$) db 0 <br/> DW 0xaa55