本文介绍了软盘扇区数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解为什么在此图像创建器中使用lseek().为什么距文件开头5个字节?如果我更改了该号码,则操作系统将无法启动.

I am trying to understand why lseek() is used in this image creator. Why 5 bytes away from start of file? If I changed that number, the OS won't boot.

图像创建者使用内部的bootloader.bin创建一个.img文件.

The image creator creates a .img file with the bootloader.bin inside.

/* modify the sector count */



total_sector_number = file_size / 512

lseek(disk_image_fd, 5, SEEK_SET);
write(disk_image_fd, &total_sector_number, 2);
write(disk_image_fd, &kernel_32_sector_number, 2);

//printf("%d\n", lawl);
printf("TOTAL_SECTOR_NUMBER : %d\n", total_sector_number);
printf("KERNEL_32_SECTOR_NUMBER : %d\n", kernel_32_sector_number);

源代码(图像制作者): http://pastebin.com/raw.php?i=MuDpYP3Y

The source code (image maker):http://pastebin.com/raw.php?i=MuDpYP3Y

引导程序: http://pastebin.com/raw.php?i=kzw2ZaU1

具有lseek()的hexdump并将扇区数写入偏移量5的字节:

The hexdump with lseek() and writing umber of sectors to byte at offset 5:

没有lseek()操作系统无法正确启动.

Without lseek() OS does not boot correctly.

推荐答案

我仅是因为您之前的帖子引导加载程序的内存位置,其中包含引导加载程序的不同源代码.

I only figured this out because of your previous post Bootloader memory location which contained different source code for the bootloader.

您提到了两个未知变量TOTALSECTORCOUNTKERNEL32SECTORCOUNT.这些变量接近文件的开头,我想在汇编时它们位于二进制文件中5个字节.使用SEEK_SET参数调用lseek会将文件指针移到文件开始之后的5个字节.然后,它将写入两个值,这些值将覆盖引导加载程序代码中的值.

You mentioned the two unknown variables TOTALSECTORCOUNT and KERNEL32SECTORCOUNT. These variables were near the beginning of the file, and I guess when assembled they sit 5 bytes into the binary. Calling lseek with the SEEK_SET parameter moves the file pointer to 5 bytes after the beginning of the file. It then writes the two values which will overwrite the ones in the bootloader code.

当您删除lseek时,它将把两个值附加到文件末尾.如果将lseek的offset参数更改为零,则会覆盖引导加载程序的jmp命令.

When you remove the lseek it will instead append the two values to the end of the file. If you changed the offset parameter of lseek to zero it would overwrite the jmp command of the bootloader instead.

在十六进制转储中注意.

Notice in your hexdump.

00000000 00eb b8fa 02c0 0000 c000 e08e e88e 00b8
                     ^    ^- kernel_32_sector_number is never initialized.
                     |-total_sector_number which was calculated in code before the write.

这篇关于软盘扇区数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 03:05