ext4_journal: two changes below.
[lwext4.git] / lwext4 / ext4_mbr.c
index 30a8e67cdd4435446a6bf20c8e4d5ca5cd17834a..1334646c2eebab0b736cbf168b9e0e4333ed68c7 100644 (file)
  */
 
 #include "ext4_config.h"
+#include "ext4_types.h"
+#include "ext4_debug.h"
 #include "ext4_mbr.h"
 
+#include <inttypes.h>
+#include <string.h>
+
 #define MBR_SIGNATURE 0xAA55
 
 struct ext4_part_entry {
@@ -55,6 +60,66 @@ struct ext4_mbr {
 } __attribute__((packed));
 
 
+int ext4_mbr_scan(struct ext4_blockdev *parent, struct ext4_mbr_bdevs *bdevs)
+{
+       int r;
+       size_t i;
+
+       ext4_dbg(DEBUG_MBR, DBG_INFO "ext4_mbr_scan\n");
+       memset(bdevs, 0, sizeof(struct ext4_mbr_bdevs));
+       r = ext4_block_init(parent);
+       if (r != EOK)
+               return r;
+
+       r = ext4_block_readbytes(parent, 0, parent->bdif->ph_bbuf, 512);
+       if (r != EOK) {
+               goto blockdev_fini;
+       }
+
+       const struct ext4_mbr *mbr = (void *)parent->bdif->ph_bbuf;
+
+       if (to_le16(mbr->signature) != MBR_SIGNATURE) {
+               ext4_dbg(DEBUG_MBR, DBG_ERROR "ext4_mbr_scan: unknown "
+                        "signature: 0x%x\n", to_le16(mbr->signature));
+               r = ENOENT;
+               goto blockdev_fini;
+       }
+
+       /*Show bootstrap code*/
+       ext4_dbg(DEBUG_MBR, "mbr_part: bootstrap:");
+       for (i = 0; i < sizeof(mbr->bootstrap); ++i) {
+               if (!(i & 0xF))
+                               ext4_dbg(DEBUG_MBR | DEBUG_NOPREFIX, "\n");
+               ext4_dbg(DEBUG_MBR | DEBUG_NOPREFIX, "%02x, ", mbr->bootstrap[i]);
+       }
+
+       ext4_dbg(DEBUG_MBR | DEBUG_NOPREFIX, "\n\n");
+       for (i = 0; i < 4; ++i) {
+               const struct ext4_part_entry *pe = &mbr->part_entry[i];
+               ext4_dbg(DEBUG_MBR, "mbr_part: %d\n", (int)i);
+               ext4_dbg(DEBUG_MBR, "\tstatus: 0x%x\n", pe->status);
+               ext4_dbg(DEBUG_MBR, "\ttype 0x%x:\n", pe->type);
+               ext4_dbg(DEBUG_MBR, "\tfirst_lba: 0x%"PRIx32"\n", pe->first_lba);
+               ext4_dbg(DEBUG_MBR, "\tsectors: 0x%"PRIx32"\n", pe->sectors);
+
+               if (!pe->sectors)
+                       continue; /*Empty entry*/
+
+               if (pe->type != 0x83)
+                       continue; /*Unsupported entry. 0x83 - linux native*/
+
+               bdevs->partitions[i].bdif = parent->bdif;
+               bdevs->partitions[i].part_offset =
+                       (uint64_t)pe->first_lba * parent->bdif->ph_bsize;
+               bdevs->partitions[i].part_size =
+                       (uint64_t)pe->sectors * parent->bdif->ph_bsize;
+       }
+
+       blockdev_fini:
+       ext4_block_fini(parent);
+       return r;
+}
+
 /**
  * @}
  */