luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

partition.h (4504B)


      1 /* Partition Table Handler for LuajitOS
      2  *
      3  * Implements MBR partition table reading/writing
      4  * Used for FDE boot partition layout
      5  */
      6 
      7 #ifndef PARTITION_H
      8 #define PARTITION_H
      9 
     10 #include <stdint.h>
     11 
     12 #ifdef __cplusplus
     13 extern "C" {
     14 #endif
     15 
     16 /* Partition types */
     17 #define PART_TYPE_EMPTY     0x00
     18 #define PART_TYPE_FAT16     0x06  /* FAT16 (32MB-2GB) */
     19 #define PART_TYPE_LUAJITOS  0x4C  /* 'L' - LuajitOS encrypted partition */
     20 #define PART_TYPE_LINUX     0x83  /* Linux native */
     21 
     22 /* MBR constants */
     23 #define MBR_SIGNATURE       0xAA55
     24 #define MBR_PART_OFFSET     446   /* Partition table starts at byte 446 */
     25 #define MBR_MAX_PARTITIONS  4
     26 
     27 /* Partition entry (16 bytes) */
     28 typedef struct __attribute__((packed)) {
     29     uint8_t  bootable;      /* 0x80 = bootable, 0x00 = not */
     30     uint8_t  start_head;    /* CHS start - head */
     31     uint8_t  start_sector;  /* CHS start - sector (bits 0-5), cylinder high (bits 6-7) */
     32     uint8_t  start_cyl;     /* CHS start - cylinder low */
     33     uint8_t  type;          /* Partition type */
     34     uint8_t  end_head;      /* CHS end - head */
     35     uint8_t  end_sector;    /* CHS end - sector */
     36     uint8_t  end_cyl;       /* CHS end - cylinder */
     37     uint32_t lba_start;     /* LBA start sector */
     38     uint32_t sector_count;  /* Number of sectors */
     39 } partition_entry_t;
     40 
     41 /* MBR structure (512 bytes) */
     42 typedef struct __attribute__((packed)) {
     43     uint8_t           bootstrap[446];   /* Boot code */
     44     partition_entry_t partitions[4];    /* Partition table */
     45     uint16_t          signature;        /* 0xAA55 */
     46 } mbr_t;
     47 
     48 /* Partition info (user-friendly) */
     49 typedef struct {
     50     uint8_t  exists;        /* Partition exists */
     51     uint8_t  bootable;      /* Is bootable */
     52     uint8_t  type;          /* Partition type */
     53     uint32_t start_lba;     /* Start sector */
     54     uint32_t sector_count;  /* Size in sectors */
     55     uint32_t size_mb;       /* Size in MB */
     56 } partition_info_t;
     57 
     58 /* ============================================================================
     59  * API Functions
     60  * ========================================================================= */
     61 
     62 /**
     63  * Read partition table from disk
     64  *
     65  * @param bus       ATA bus
     66  * @param drive     ATA drive
     67  * @param parts     Array of 4 partition_info_t to fill
     68  * @return          0 on success, -1 on error
     69  */
     70 int partition_read_table(uint8_t bus, uint8_t drive, partition_info_t parts[4]);
     71 
     72 /**
     73  * Write partition table to disk
     74  * WARNING: Destroys all data on disk!
     75  *
     76  * @param bus       ATA bus
     77  * @param drive     ATA drive
     78  * @param parts     Array of 4 partition entries to write
     79  * @return          0 on success, -1 on error
     80  */
     81 int partition_write_table(uint8_t bus, uint8_t drive, const partition_entry_t parts[4]);
     82 
     83 /**
     84  * Check if disk has valid MBR
     85  *
     86  * @param bus       ATA bus
     87  * @param drive     ATA drive
     88  * @return          1 if valid MBR, 0 otherwise
     89  */
     90 int partition_has_mbr(uint8_t bus, uint8_t drive);
     91 
     92 /**
     93  * Create LuajitOS partition layout
     94  * Creates: 16MB boot partition + encrypted data partition
     95  *
     96  * @param bus           ATA bus
     97  * @param drive         ATA drive
     98  * @param boot_size_mb  Boot partition size in MB (recommended: 16-32)
     99  * @return              0 on success, -1 on error
    100  */
    101 int partition_create_luajitos_layout(uint8_t bus, uint8_t drive, uint32_t boot_size_mb);
    102 
    103 /**
    104  * Get partition info by index
    105  *
    106  * @param bus       ATA bus
    107  * @param drive     ATA drive
    108  * @param index     Partition index (0-3)
    109  * @param info      Output partition info
    110  * @return          0 on success, -1 if partition doesn't exist
    111  */
    112 int partition_get_info(uint8_t bus, uint8_t drive, int index, partition_info_t *info);
    113 
    114 /**
    115  * Get the start sector of a partition
    116  *
    117  * @param bus       ATA bus
    118  * @param drive     ATA drive
    119  * @param index     Partition index (0-3)
    120  * @return          Start LBA, or 0 if partition doesn't exist
    121  */
    122 uint32_t partition_get_start(uint8_t bus, uint8_t drive, int index);
    123 
    124 /**
    125  * Get the size of a partition in sectors
    126  *
    127  * @param bus       ATA bus
    128  * @param drive     ATA drive
    129  * @param index     Partition index (0-3)
    130  * @return          Sector count, or 0 if partition doesn't exist
    131  */
    132 uint32_t partition_get_size(uint8_t bus, uint8_t drive, int index);
    133 
    134 /* ============================================================================
    135  * Lua Bindings
    136  * ========================================================================= */
    137 
    138 struct lua_State;
    139 int luaopen_partition(struct lua_State *L);
    140 
    141 #ifdef __cplusplus
    142 }
    143 #endif
    144 
    145 #endif /* PARTITION_H */