Introduce MBR struct; simplify cdrom emulation code.
authorKevin O'Connor <kevin@koconnor.net>
Sat, 7 Feb 2009 05:04:57 +0000 (00:04 -0500)
committerKevin O'Connor <kevin@koconnor.net>
Sat, 7 Feb 2009 05:04:57 +0000 (00:04 -0500)
Define and use a struct for the master boot record.
Simplify cdrom emulation chs setting code.
Fix an apparent bug in harddrive chs setting - it wasn't properly
    masking the spt/cyl fields.

src/boot.c
src/cdrom.c
src/disk.h

index 1f0647a80bc2337d060e2dd7473a5c9b07b4b4f0..6db213a43e8b78d081cee4f8bac6f786a2428bbc 100644 (file)
@@ -130,7 +130,8 @@ try_boot(u16 seq_nr)
         /* Always check the signature on a HDD boot sector; on FDD,
          * only do the check if configured for it */
         if (type != IPL_TYPE_FLOPPY || IPL.checkfloppysig) {
-            if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
+            struct mbr_s *mbr = (void*)0;
+            if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
                 print_boot_failure(type, 0);
                 return;
             }
index fdef873ba84a2c20ed3af7def5ef1712a0c8af5f..389a42e10612334b211b81a324d21b8f4245c34a 100644 (file)
@@ -519,38 +519,38 @@ cdrom_boot()
         // Floppy emulation
         SET_EBDA2(ebda_seg, cdemu.emulated_drive, 0x00);
         SETBITS_BDA(equipment_list_flags, 0x41);
+
+        switch (media) {
+        case 0x01:  // 1.2M floppy
+            SET_EBDA2(ebda_seg, cdemu.spt, 15);
+            SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
+            SET_EBDA2(ebda_seg, cdemu.heads, 2);
+            break;
+        case 0x02:  // 1.44M floppy
+            SET_EBDA2(ebda_seg, cdemu.spt, 18);
+            SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
+            SET_EBDA2(ebda_seg, cdemu.heads, 2);
+            break;
+        case 0x03:  // 2.88M floppy
+            SET_EBDA2(ebda_seg, cdemu.spt, 36);
+            SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
+            SET_EBDA2(ebda_seg, cdemu.heads, 2);
+            break;
+        }
     } else {
         // Harddrive emulation
         SET_EBDA2(ebda_seg, cdemu.emulated_drive, 0x80);
         SET_BDA(hdcount, GET_BDA(hdcount) + 1);
-    }
 
-    // Remember the media type
-    switch (media) {
-    case 0x01:  // 1.2M floppy
-        SET_EBDA2(ebda_seg, cdemu.spt, 15);
-        SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
-        SET_EBDA2(ebda_seg, cdemu.heads, 2);
-        break;
-    case 0x02:  // 1.44M floppy
-        SET_EBDA2(ebda_seg, cdemu.spt, 18);
-        SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
-        SET_EBDA2(ebda_seg, cdemu.heads, 2);
-        break;
-    case 0x03:  // 2.88M floppy
-        SET_EBDA2(ebda_seg, cdemu.spt, 36);
-        SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
-        SET_EBDA2(ebda_seg, cdemu.heads, 2);
-        break;
-    case 0x04: { // Harddrive
-        u16 spt = GET_FARVAR(boot_segment,*(u8*)(446+6));
-        u16 cyl = (spt << 2) + GET_FARVAR(boot_segment,*(u8*)(446+7)) + 1;
-        u16 heads = GET_FARVAR(boot_segment,*(u8*)(446+5)) + 1;
-        SET_EBDA2(ebda_seg, cdemu.spt, spt & 0x3f);
-        SET_EBDA2(ebda_seg, cdemu.cylinders, cyl);
-        SET_EBDA2(ebda_seg, cdemu.heads, heads);
-        break;
-    }
+        // Peak at partition table to get chs.
+        struct mbr_s *mbr = (void*)0;
+        u8 sptcyl = GET_FARVAR(boot_segment, mbr->partitions[0].last.sptcyl);
+        u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
+        u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
+
+        SET_EBDA2(ebda_seg, cdemu.spt, sptcyl & 0x3f);
+        SET_EBDA2(ebda_seg, cdemu.cylinders, ((sptcyl<<2)&0x300) + cyllow + 1);
+        SET_EBDA2(ebda_seg, cdemu.heads, heads + 1);
     }
 
     // everything is ok, so from now on, the emulation is active
index fae66e3b4eae41acdd808a5f82c59859373a8040..9a4bf37153233f5b2f3ad4bf33971fbf93bbd3a5 100644 (file)
@@ -103,6 +103,40 @@ void __disk_ret(struct bregs *regs, u32 linecode, const char *fname);
     __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
 
 
+/****************************************************************
+ * Master boot record
+ ****************************************************************/
+
+struct packed_chs_s {
+    u8 heads;
+    u8 sptcyl;
+    u8 cyllow;
+};
+
+struct partition_s {
+    u8 status;
+    struct packed_chs_s first;
+    u8 type;
+    struct packed_chs_s last;
+    u32 lba;
+    u32 count;
+} PACKED;
+
+struct mbr_s {
+    u8 code[440];
+    // 0x01b8
+    u32 diskseg;
+    // 0x01bc
+    u16 null;
+    // 0x01be
+    struct partition_s partitions[4];
+    // 0x01fe
+    u16 signature;
+} PACKED;
+
+#define MBR_SIGNATURE 0xaa55
+
+
 /****************************************************************
  * Disk command request
  ****************************************************************/