[haiku-commits] r35717 - haiku/trunk/src/tools/anyboot

  • From: mmlr@xxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 2 Mar 2010 02:22:11 +0100 (CET)

Author: mmlr
Date: 2010-03-02 02:22:11 +0100 (Tue, 02 Mar 2010)
New Revision: 35717
Changeset: http://dev.haiku-os.org/changeset/35717/haiku

Modified:
   haiku/trunk/src/tools/anyboot/anyboot.cpp
Log:
Add a CHS conversion function. It is not used though as we can't generally make
valid CHS addresses due to not knowing the geometry of the target device
beforehand. So it's mostly just for completeness...


Modified: haiku/trunk/src/tools/anyboot/anyboot.cpp
===================================================================
--- haiku/trunk/src/tools/anyboot/anyboot.cpp   2010-03-02 00:53:41 UTC (rev 
35716)
+++ haiku/trunk/src/tools/anyboot/anyboot.cpp   2010-03-02 01:22:11 UTC (rev 
35717)
@@ -44,6 +44,41 @@
 }
 
 
+void
+chsAddressFor(uint32_t offset, uint8_t *address, uint32_t sectorsPerTrack,
+       uint32_t headsPerCylinder)
+{
+       if (offset >= 1024 * sectorsPerTrack * headsPerCylinder) {
+               // not encodable, force LBA
+               address[0] = 0xff;
+               address[1] = 0xff;
+               address[2] = 0xff;
+               return;
+       }
+
+       uint32_t cylinders = 0;
+       uint32_t heads = 0;
+       uint32_t sectors = 0;
+
+       uint32_t temp = 0;
+       while (temp * sectorsPerTrack * headsPerCylinder <= offset)
+               cylinders = temp++;
+
+       offset -= (sectorsPerTrack * headsPerCylinder * cylinders);
+
+       temp = 0;
+       while (temp * sectorsPerTrack <= offset)
+               heads = temp++;
+
+       sectors = offset - (sectorsPerTrack * heads) + 1;
+
+       address[0] = heads;
+       address[1] = sectors;
+       address[1] |= (cylinders >> 2) & 0xc0;
+       address[2] = cylinders;
+}
+
+
 int
 main(int argc, char *argv[])
 {
@@ -97,11 +132,9 @@
        // construct the partition table
        uint8_t partition[16] = {
                0x80,                                   // active
-               0xff, 0xff, 0xff,               // CHS start offset
-                       // TODO: we enforce LBA here, maybe not a good idea?
+               0xff, 0xff, 0xff,               // CHS first block (default to 
LBA)
                0xeb,                                   // partition type (BFS)
-               0xff, 0xff, 0xff,               // CHS end offset
-                       // TODO: see above
+               0xff, 0xff, 0xff,               // CHS last block (default to 
LBA)
                0x00, 0x00, 0x00, 0x00, // imageOffset in blocks (written below)
                0x00, 0x00, 0x00, 0x00  // imageSize in blocks (written below)
        };
@@ -109,9 +142,26 @@
        alignment = kBlockSize - 1;
        imageSize = (imageSize + alignment) & ~alignment;
 
+       // fill in LBA values
        uint32_t partitionOffset = (uint32_t)(imageOffset / kBlockSize);
        ((uint32_t *)partition)[2] = partitionOffset;
        ((uint32_t *)partition)[3] = (uint32_t)(imageSize / kBlockSize);
+
+#if 0
+       // while this should basically work, it makes the boot code needlessly
+       // use chs which has a high potential of failure due to the geometry
+       // being unknown beforehand (a fixed geometry would be used here).
+
+       uint32_t sectorsPerTrack = 63;
+       uint32_t headsPerCylinder = 255;
+
+       // fill in CHS values
+       chsAddressFor(partitionOffset, &partition[1], sectorsPerTrack,
+               headsPerCylinder);
+       chsAddressFor(partitionOffset + (uint32_t)(imageSize / kBlockSize),
+               &partition[5], sectorsPerTrack, headsPerCylinder);
+#endif
+
        ssize_t written = pwrite(outputFile, partition, 16, 512 - 2 - 16 * 4);
        checkError(written != 16, "failed to write partition entry");
 


Other related posts:

  • » [haiku-commits] r35717 - haiku/trunk/src/tools/anyboot - mmlr