Unable to get FAT16 boot loader working

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@neon: Thanks! I used WinHex to get the Hex dump of my hard drive image while it was mounted to my I drive. Below is the dump of my BPB (I am including the first 64 bytes which includes BPB and additional instructions):
00000000 EB 3C 90 4D 53 44 4F 53 35 2E 30 00 02 01 06 00 ë< MSDOS5.0
00000010 02 00 02 80 9D F8 9D 00 3F 00 08 00 01 00 00 00 € ø ?
00000020 00 00 00 00 80 00 29 20 E1 D1 DC 4E 4F 20 4E 41 € ) áÑÜNO NA
00000030 4D 45 20 20 20 20 46 41 54 31 36 20 20 20 87 DB ME FAT16 ‡Û
00000040 31 C0 8E D8 8E D0 BC 00 7C 89 E5 88 56 24 8A 46 1ÀŽØŽÐ¼ |‰åˆV$ŠF
I already had ImDisk on my machine. I will look into Gizmo.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Unable to get FAT16 boot loader working

Post by neon »

ImDisk itself works fine with producing images that can be used with Bochs. Shouldn't need anything else. We ran the hex dump through our software to validate it, this is what we got:

total sectors: 36992
sectors per track: 63
head count: 8
bytes per sector: 512
c = ⌊36992 / (8*63)⌋ = 73

So, spt=63 heads=8 cylinders=73. Think thats right.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@neon: Thanks however when I updated my bochsrc.bxrc file with the information that you provided, the int 13h sets the carry flag is set (AH=01h - invalid function in AH or invalid parameter).

I created a new image using ImDisk (the UI is very similar to OSFMount) however still am unable to get any boot loader working :-k

I don't understand why I am having so much trouble getting this to work!
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

Just because by some of your posts it seems you have an MBR / Partition Table in the first sector of your HDD

One of your posts stated "sc_b4_prt: ; 1Ch
dd 16065 ; partition 1 (CHS=1:0:1) "

But how you are copying the Boot Sector is to the first sector of the drive (MBR) not the first sector of the active partition (VBR)

Standard MBR from microsoft loads VBR to 07C0:0000 and jumps to it.
dl = drive letter
DS:SI = pointer to partition table entry



And also note that I do not trust the Hidden Sectors field in BPB (it is giving me issues here) So it could also be your issue.

Since you are using someone else's code already I'll be happy to share mine once I get the bugs ironed out. (Won't share my stage two, as It is not to any Standard but mine :lol: <-there needs to be an evil laugh smilie)
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@BASICFreak: You are correct, my MBR is on the first sector of my HDD. I am copying it using partcopy. As per the aurthor's instructions:
; Then you must copy the first three bytes of BOOT16.BIN to the first three
; bytes of the volume and copy bytes 0x3E through 0x1FF of BOOT16.BIN to
; bytes 0x3E through 0x1FF of the volume. Bytes 0x3 through 0x3D of the
; volume should be set by a FAT16 format program and should not be modified
; when copying boot16.bin to the volume.
;
; If you use my PARTCOPY program to install BOOT16.BIN on D:, the
; commands are:
;
; partcopy boot16.bin 0 3 -ad
; partcopy boot16.bin 3e 1c2 -ad 3e
I have verified via Bochs that the code in boot16.asm is being hit via the Magic Breakpoint and it is loaded into memory at 07C0:0000 (linear address 0x00007C00).

I would be interested in trying your boot loader. I already have a stage two loader ;)
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

Again, the boot sector needs to be at the start of the partition not the start of the disk

So Redo your image allow MS to place MBR then install boot sector to sector 16065 (assuming the location of the partition in your previous posts are correct)

Then try to load the image, see if that works.

Again it seems to me that you are over writing the MBR while you have a partitioned drive. The MBR loads the VBR which is the first sector of the partition.

Writing the Boot Loader into MBR can only be done on a flat disk (such as floppy), not MBR disk.




NOTE: I am switching between boot loader and UI so it may be a day or two for my boot loader to be finished
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@BASICFreak: That probably explains all of my issues! Could you please tell me how you came up with the offset for the VBR and how you copy your first stage boot loader to your disk? Thanks again.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

Check out these links:
http://wiki.osdev.org/MBR_(x86)
http://wiki.osdev.org/Partition_Table


I found the info I gave you from your previous posts where you set sc_b4_prt to 16065 (again that was assuming you were right)

But the start of the Partition will be found in the partition table:
OSDev Wiki wrote:Each of the four Partition Table entries contains the following elements, in the following structure:
Element (offset) Size Description
0 byte Boot indicator bit flag: 0 = no, 0x80 = bootable (or "active")
1 byte Starting head
2 6 bits Starting sector (Bits 6-7 are the upper two bits for the Starting Cylinder field.)
3 10 bits Starting Cylinder
4 byte System ID
5 byte Ending Head
6 6 bits Ending Sector (Bits 6-7 are the upper two bits for the ending cylinder field)
7 10 bits Ending Cylinder
8 uint32_t Relative Sector (to start of partition -- also equals the partition's starting LBA value)
12 uint32_t Total Sectors in partition
EDIT:
100 Posts 8)

Also to copy my boot sector to disk I boot from my floppy image which runs:

Code: Select all

void *NEWBS = calloc(512);
void *HDDBS = calloc(512);
uint32_t HDDPID = GetPartByName("BOSHDD     ");
FILE *FAT16BS = fopen("/BOS v 0.0.3/data/fat16bs.bin");
if(HDDPID != 0xFFFFFFFF && FAT16BS->Length == 512) {
	fread(FAT16BS, 0, NEWBS, 512);
	PartRead(HDDPID, 0, HDDBS, 1);
	memcpy(HDDBS, NEWBS, 3);
	memcpy((void*)(((uint32_t)HDDBS) + 0x3E), (void*)(((uint32_t)NEWBS) + 0x3E), 0x1C2);
	PartWrite(HDDPID, 0, HDDBS, 1);
}
fclose(FAT16BS);
free(HDDBS, 512);
free(NEWBS, 512);
So I write the VBR from my OS
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@BASICFreak: Ok, I think I am starting to finally understand this. BTW I am able to use WinHex to parse apart the boot sector:

Code: Select all

Offset  Title                       Value
00      JMP instruction             EB 3C 90
03      OEM                         MSDOS5.0
0B      Bytes per sector            512
0D      Sectors per cluster         1
0E      Reserved sectors            6
10      Number of FATs              2
11      Root entries                512
13      Sectors (under 32 MB)       40,320
15      Media descriptor (hex)      F8
16      Sectors per FAT             157
18      Sectors per track           63
1A      Heads                       8
1C      Hidden sectors              1
20      Sectors (over 32 MB)        0
24      BIOS drive (hex, HD=8x)     80
25      (Unused)                    0
26      Ext. boot signature (29h)   29
27      Volume serial number (hex)  96 10 61 D8
2B      Volume label                NO NAME    
36      File system                 FAT16   
1FE     Signature (55 AA)           55 AA
There are two items of interest. The off, the sc_b4_prt was from a hardcoded example so you can scratch that value. The value from my 20 MB hard disk is simply 1. In that case were do I need to put my boot loader code?

Also when I created the drive in bximage it reported my C/H/S as 40/16/63. However the BPB above says that I only have 8 heads not 16. Does it make a different?
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

vbguyny wrote:@BASICFreak: Ok, I think I am starting to finally understand this. BTW I am able to use WinHex to parse apart the boot sector:

Code: Select all

Offset  Title                       Value
00      JMP instruction             EB 3C 90
03      OEM                         MSDOS5.0
0B      Bytes per sector            512
0D      Sectors per cluster         1
0E      Reserved sectors            6
10      Number of FATs              2
11      Root entries                512
13      Sectors (under 32 MB)       40,320
15      Media descriptor (hex)      F8
16      Sectors per FAT             157
18      Sectors per track           63
1A      Heads                       8
1C      Hidden sectors              1
20      Sectors (over 32 MB)        0
24      BIOS drive (hex, HD=8x)     80
25      (Unused)                    0
26      Ext. boot signature (29h)   29
27      Volume serial number (hex)  96 10 61 D8
2B      Volume label                NO NAME    
36      File system                 FAT16   
1FE     Signature (55 AA)           55 AA
There are two items of interest. The off, the sc_b4_prt was from a hardcoded example so you can scratch that value. The value from my 20 MB hard disk is simply 1. In that case were do I need to put my boot loader code?

Also when I created the drive in bximage it reported my C/H/S as 40/16/63. However the BPB above says that I only have 8 heads not 16. Does it make a different?
Microsoft puts whatever they want in the values, not necessarily what the BIOS reports (always use int 13h ah 02h to get true CHS)

According to the information you have provided the start of the first partition is sector 1 (the second sector). Though that does seem strange to me as I have never seen MS put less than 3F (start of next cyl)

If the BPB is correct the start of the partition is "Hidden Sectors".
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

I had figured out my issue!!! It all boils down to the fact that I was formatting my hard drive image from Windows 7. What I needed to do was instead format it from MS-DOS. Below are the steps that I took to create a booting drive in FAT16:

(Note: Used DOSBox, Bochs 2.6.6, and ImDisk, as well as MS-DOS 6.22 image)

1. Use bximage from Bochs folder to create a new disk. (Take note of the C/H/S values)[/list]
2. Ran DOSBox with the following commnds:
2a. imgmount 2 "c.img" -size 512,63,16,40 -t hdd -fs none (The size values need to be populated based on the return value from bximage)
2b. boot 622c.img
3. Once in MS-DOS run fdisk and create the primary partition. This will cause you to leave DOSBox
4. Go back into DOSBox and run the imgmount again
5. format c: and close out of DOSBox
6. Mount the image using ImDisk leaving the defaults on the "mount new virtual disk" dialog
7. Use partcopy to copy over your custom MBR
7a. partcopy boot.bin 3e 1c2 -ai 3e
8. Copy over your 2nd stage boot loader
9. Unmount the drive

Also re-examined the boot sector and below are the new values of my BPB for a 20 MB drive:

Code: Select all

Offset  Title                       Value
00      JMP instruction             EB 3C 90
03      OEM                         MSDOS5.0
0B      Bytes per sector            512
0D      Sectors per cluster         4
0E      Reserved sectors            1
10      Number of FATs              2
11      Root entries                512
13      Sectors (under 32 MB)       40,257
15      Media descriptor (hex)      F8
16      Sectors per FAT             40
18      Sectors per track           63
1A      Heads                       16
1C      Hidden sectors              63
20      Sectors (over 32 MB)        0
24      BIOS drive (hex, HD=8x)     80
25      (Unused)                    0
26      Ext. boot signature (29h)   29
27      Volume serial number (hex)  1A 1C 3D 16
2B      Volume label                ABCDEFG    
36      File system                 FAT16   
1FE     Signature (55 AA)           55 AA
Thanks again for your guys help!
madanra
Member
Member
Posts: 149
Joined: Mon Sep 07, 2009 12:01 pm

Re: Unable to get FAT16 boot loader working

Post by madanra »

If your bootloader breaks when you format the partition with Windows 7, but works with DOS formatting, that suggests there are still bugs in your bootloader.
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: Unable to get FAT16 boot loader working

Post by Octocontrabass »

I notice that the DOS-formatted BPB says 16 heads, the same value you were telling Bochs. Now that the BPB geometry matches the geometry you're telling Bochs, it works.

This wouldn't be an issue at all if your bootloader used INT 13h AH=08h. That function asks the BIOS for the correct geometry instead of trusting whatever is in the BPB.

The DOS MBR has the same issue as the FAT16 bootloader you're trying to use. The Windows 7 MBR does not.
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Unable to get FAT16 boot loader working

Post by BASICFreak »

Octocontrabass wrote:This wouldn't be an issue at all if your bootloader used INT 13h AH=08h. That function asks the BIOS for the correct geometry instead of trusting whatever is in the BPB.
That's the INT I meant to post...



Anyways good to see you have it loading.
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
vbguyny
Member
Member
Posts: 48
Joined: Wed Aug 12, 2015 6:30 pm

Re: Unable to get FAT16 boot loader working

Post by vbguyny »

@Octocontrabass: Unbelievable! What is the point of the BPB if it can't be trusted?! Thanks for the info.
Post Reply