Changes in / [283ea3d:8559fa0] in mainline


Ignore:
Location:
uspace
Files:
2 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/hdisk/func_gpt.c

    r283ea3d r8559fa0  
    4242#include "input.h"
    4343
    44 static int set_gpt_partition(tinput_t *, gpt_part_t *, unsigned int);
     44static int set_gpt_partition(tinput_t *, gpt_part_t *);
     45
    4546
    4647int construct_gpt_label(label_t *this)
     
    4950        this->alignment = 1;
    5051       
    51         this->add_part      = add_gpt_part;
    52         this->delete_part   = delete_gpt_part;
    53         this->destroy_label = destroy_gpt_label;
    54         this->new_label     = new_gpt_label;
    55         this->print_parts   = print_gpt_parts;
    56         this->read_parts    = read_gpt_parts;
    57         this->write_parts   = write_gpt_parts;
    58         this->extra_funcs   = extra_gpt_funcs;
     52        this->add_part    = add_gpt_part;
     53        this->delete_part = delete_gpt_part;
     54        this->new_label   = new_gpt_label;
     55        this->print_parts = print_gpt_parts;
     56        this->read_parts  = read_gpt_parts;
     57        this->write_parts = write_gpt_parts;
     58        this->extra_funcs = extra_gpt_funcs;
    5959       
    6060        return this->new_label(this);
     
    6868        }
    6969       
    70         return set_gpt_partition(in, p, this->alignment);
     70        return set_gpt_partition(in, p);
    7171}
    7272
     
    8080       
    8181        rc = gpt_remove_partition(this->data.gpt, idx);
    82         if (rc == ENOMEM) {
     82        if (rc != EOK) {
    8383                printf("Warning: running low on memory, not resizing...\n");
    8484                return rc;
    85         } else if (rc == EINVAL) {
    86                 printf("Invalid index.\n");
    87                 return rc;
    8885        }
    8986       
     
    105102int print_gpt_parts(label_t *this)
    106103{
     104        //int rc;
    107105        printf("Current partition scheme (GPT):\n");
    108         printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
     106        printf("\t\tStart:\tEnd:\tLength:\tType:\tName:\n");
    109107       
    110108        size_t i = 0;
     
    112110        gpt_part_foreach(this->data.gpt, iter) {
    113111                i++;
    114                
     112                //FIXMEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    115113                if (gpt_get_part_type(iter) == GPT_PTE_UNUSED)
    116114                        continue;
     
    119117                        printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
    120118               
    121                
     119                //printf("\t%10u %10u %10u %3d\n", iter->start_addr, iter->start_addr + iter->length,
     120                //              iter->length, gpt_get_part_type(iter), gpt_get_part_name(iter));
    122121                printf("%3u  %10llu %10llu %10llu    %3d %s\n", i-1, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
    123122                                gpt_get_end_lba(iter) - gpt_get_start_lba(iter), gpt_get_part_type(iter),
    124123                                gpt_get_part_name(iter));
    125                
    126         }
    127        
     124        }
     125       
     126        //return rc;
    128127        return EOK;
    129128}
     
    173172}
    174173
    175 static int set_gpt_partition(tinput_t *in, gpt_part_t *p, unsigned int alignment)
     174static int set_gpt_partition(tinput_t *in, gpt_part_t *p)
    176175{
    177176        int rc;
     
    181180        printf("Set starting address (number): ");
    182181        sa = get_input_uint64(in);
    183         if (sa % alignment != 0)
    184                 sa = gpt_get_next_aligned(sa, alignment);
    185182       
    186183        printf("Set end addres (number): ");
     
    195192        gpt_set_end_lba(p, ea);
    196193       
    197         //printf("Set type : ");
    198         //size_t idx = get_input_size_t(in);
    199         //gpt_set_part_type(p, idx);
    200        
    201         gpt_set_random_uuid(p->part_type);
    202         gpt_set_random_uuid(p->part_id);
    203194       
    204195        char *name;
     
    210201        }
    211202       
     203        printf("name: %s, len: %d\n", name, str_size(name));
    212204        gpt_set_part_name(p, name, str_size(name));
    213205       
  • uspace/app/hdisk/hdisk.c

    r283ea3d r8559fa0  
    144144        rc = interact(dev_handle);
    145145       
     146        free_label();
     147       
    146148        return rc;
    147149}
     
    178180                                break;
    179181                        case 'f':
     182                                free_label();
    180183                                select_label_format(in);
    181184                                break;
  • uspace/lib/gpt/libgpt.c

    r283ea3d r8559fa0  
    5151#include "libgpt.h"
    5252
    53 static int load_and_check_header(service_id_t, aoff64_t, size_t, gpt_header_t *);
    54 static gpt_partitions_t * alloc_part_array(uint32_t);
     53static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t *header);
     54static gpt_partitions_t * alloc_part_array(uint32_t num);
    5555static int extend_part_array(gpt_partitions_t *);
    5656static int reduce_part_array(gpt_partitions_t *);
    57 //static long long nearest_larger_int(double);
     57static long long nearest_larger_int(double a);
    5858static uint8_t get_byte(const char *);
    59 static bool check_overlap(gpt_part_t *, gpt_part_t *);
     59static int check_overlap(gpt_part_t * p1, gpt_part_t * p2);
    6060
    6161/** Allocate memory for gpt label */
     
    6666                return NULL;
    6767       
    68         /* This is necessary so that gpt_part_foreach does not segfault */
    69         label->parts = gpt_alloc_partitions();
    70         if (label == NULL) {
    71                 free(label);
    72                 return NULL;
    73         }
    74        
    7568        label->gpt = NULL;
    76        
     69        label->parts = NULL;
    7770        label->device = 0;
    7871       
     
    9992                return NULL;
    10093       
    101         /*
    102          * We might need only sizeof(gpt_header_t), but we should follow
    103          * specs and have zeroes through all the rest of the block
    104          */
     94        // We might need only sizeof(gpt_header_t),
     95        // but we should follow specs and have
     96        // zeroes through all the rest of the block
    10597        size_t final_size = size > sizeof(gpt_header_t) ? size : sizeof(gpt_header_t);
    10698        gpt->header = malloc(final_size);
     
    186178        int rc;
    187179        size_t b_size;
    188        
    189         /* The comm_size argument (the last one) is ignored */
    190         rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
     180
     181        label->gpt->header->header_crc32 = 0;
     182        label->gpt->header->header_crc32 = compute_crc32((uint8_t *) label->gpt->header,
     183                                        uint32_t_le2host(label->gpt->header->header_size));
     184
     185        rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
    191186        if (rc != EOK && rc != EEXIST)
    192187                return rc;
    193        
     188
    194189        rc = block_get_bsize(dev_handle, &b_size);
    195190        if (rc != EOK)
    196191                return rc;
    197        
     192
     193        /* Write to main GPT header location */
     194        rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
     195        if (rc != EOK) {
     196                block_fini(dev_handle);
     197                return rc;
     198        }
     199
    198200        aoff64_t n_blocks;
    199201        rc = block_get_nblocks(dev_handle, &n_blocks);
     
    202204                return rc;
    203205        }
    204        
    205         uint64_t tmp;
    206        
    207         /* Prepare the backup header */
    208         label->gpt->header->alternate_lba = label->gpt->header->my_lba;
    209         label->gpt->header->my_lba = host2uint64_t_le(n_blocks - 1);
    210        
    211         tmp = label->gpt->header->entry_lba;
    212         label->gpt->header->entry_lba = host2uint64_t_le(n_blocks -
    213             (uint32_t_le2host(label->gpt->header->fillries) * sizeof(gpt_entry_t))
    214             / b_size - 1);
    215        
    216         label->gpt->header->header_crc32 = 0;
    217         label->gpt->header->header_crc32 = host2uint32_t_le(
    218             compute_crc32((uint8_t *) label->gpt->header,
    219                 uint32_t_le2host(label->gpt->header->header_size)));
    220        
     206
    221207        /* Write to backup GPT header location */
     208        //FIXME: those idiots thought it would be cool to have these fields in reverse order...
    222209        rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
    223         if (rc != EOK) {
    224                 block_fini(dev_handle);
     210        block_fini(dev_handle);
     211        if (rc != EOK)
    225212                return rc;
    226         }
    227        
    228        
    229         /* Prepare the main header */
    230         label->gpt->header->entry_lba = tmp;
    231        
    232         tmp = label->gpt->header->alternate_lba;
    233         label->gpt->header->alternate_lba = label->gpt->header->my_lba;
    234         label->gpt->header->my_lba = tmp;
    235        
    236         label->gpt->header->header_crc32 = 0;
    237         label->gpt->header->header_crc32 = host2uint32_t_le(
    238             compute_crc32((uint8_t *) label->gpt->header,
    239                 uint32_t_le2host(label->gpt->header->header_size)));
    240        
    241         /* Write to main GPT header location */
    242         rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
    243         block_fini(dev_handle);
    244         if (rc != EOK)
    245                 return rc;
    246        
    247        
     213
    248214        return 0;
    249215}
     
    252218gpt_partitions_t * gpt_alloc_partitions()
    253219{
    254         return alloc_part_array(GPT_MIN_PART_NUM);
     220        return alloc_part_array(128);
    255221}
    256222
     
    264230        int rc;
    265231        unsigned int i;
    266         uint32_t fillries = uint32_t_le2host(label->gpt->header->fillries);
     232        uint32_t fill = uint32_t_le2host(label->gpt->header->fillries);
    267233        uint32_t ent_size = uint32_t_le2host(label->gpt->header->entry_size);
    268234        uint64_t ent_lba = uint64_t_le2host(label->gpt->header->entry_lba);
    269235       
    270236        if (label->parts == NULL) {
    271                 label->parts = alloc_part_array(fillries);
     237                label->parts = alloc_part_array(fill);
    272238                if (label->parts == NULL) {
    273239                        return ENOMEM;
     
    275241        }
    276242
     243        /* We can limit comm_size like this:
     244         *  - we don't need more bytes
     245         *  - the size of GPT partition entry can be different to 128 bytes */
    277246        /* comm_size is ignored */
    278247        rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
     
    289258        aoff64_t pos = ent_lba * block_size;
    290259
    291         /*
    292          * Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
     260        /* Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
    293261         * Hopefully, this does not bypass cache (no mention in libblock.c),
    294262         * and also allows us to have variable partition entry size (but we
    295263         * will always read just sizeof(gpt_entry_t) bytes - hopefully they
    296          * don't break backward compatibility)
    297          */
    298         for (i = 0; i < fillries; ++i) {
    299                 /*FIXME: this does bypass cache... */
     264         * don't break backward compatibility) */
     265        for (i = 0; i < fill; ++i) {
     266                //FIXME: this does bypass cache...
    300267                rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
    301                 /*
    302                  * FIXME: but seqread() is just too complex...
    303                  * rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
    304                  */
     268                //FIXME: but seqread() is just too complex...
     269                //rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
    305270                pos += ent_size;
    306271
     
    309274        }
    310275
    311         /*
    312          * FIXME: so far my boasting about variable partition entry size
     276        /* FIXME: so far my boasting about variable partition entry size
    313277         * will not work. The CRC32 checksums will be different.
    314278         * This can't be fixed easily - we'd have to run the checksum
     
    316280         */
    317281        uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array,
    318                            fillries * sizeof(gpt_entry_t));
     282                           label->parts->fill * sizeof(gpt_entry_t));
    319283
    320284        if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
     
    348312        size_t b_size;
    349313        uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
    350         size_t fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
    351        
    352         label->gpt->header->fillries = host2uint32_t_le(fillries);
    353         label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
     314        size_t fill = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
     315       
     316        label->gpt->header->pe_array_crc32 = compute_crc32(
    354317                                       (uint8_t *) label->parts->part_array,
    355                                        fillries * e_size));
     318                                       fill * e_size);
    356319       
    357320        /* comm_size of 4096 is ignored */
     
    369332                goto fail;
    370333       
    371         uint64_t arr_blocks = (fillries * sizeof(gpt_entry_t)) / b_size;
    372         label->gpt->header->first_usable_lba = host2uint64_t_le(arr_blocks + 1);
    373         label->gpt->header->last_usable_lba = host2uint64_t_le(n_blocks - arr_blocks - 2);
    374        
    375        
    376334        /* Write to backup GPT partition array location */
    377         rc = block_write_direct(dev_handle, n_blocks - arr_blocks - 1,
    378                  arr_blocks, label->parts->part_array);
     335        //rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, header->raw_data);
    379336        if (rc != EOK)
    380337                goto fail;
     
    382339        /* Write to main GPT partition array location */
    383340        rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
    384                  arr_blocks, label->parts->part_array);
     341                        nearest_larger_int((uint64_t_le2host(label->gpt->header->entry_size) * label->parts->fill) / b_size),
     342                        label->parts->part_array);
    385343        if (rc != EOK)
    386344                goto fail;
     
    427385{
    428386        gpt_part_t *p;
    429        
    430387       
    431388        /* Find the first empty entry */
     
    483440int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
    484441{
    485         /* FIXME: Check dimensions! */
     442        if (label->parts->fill == label->parts->arr_size) {
     443                if (extend_part_array(label->parts) == -1)
     444                        return ENOMEM;
     445        }
     446       
     447        /*FIXME:
     448         * Check dimensions and stuff! */
    486449        gpt_part_foreach(label, p) {
    487450                if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
     
    491454        }
    492455       
    493         gpt_part_t *p;
    494         /* Find the first empty entry */
    495         do {
    496                 if (label->parts->fill == label->parts->arr_size) {
    497                         if (extend_part_array(label->parts) == -1)
    498                                 return ENOMEM;
    499                 }
    500                
    501                 p = label->parts->part_array + label->parts->fill++;
    502                
    503         } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
    504        
    505        
    506         memcpy(p, partition, sizeof(gpt_entry_t));
     456        memcpy(label->parts->part_array + label->parts->fill++,
     457               partition, sizeof(gpt_part_t));
     458       
    507459       
    508460       
     
    521473int gpt_remove_partition(gpt_label_t *label, size_t idx)
    522474{
    523         if (idx >= label->parts->arr_size)
     475        if (idx >= label->parts->fill)
    524476                return EINVAL;
    525477       
    526         /*
    527          * FIXME!
     478        /* FIXME!
    528479         * If we allow blank spots, we break the array. If we have more than
    529480         * 128 partitions in the array and then remove something from
    530          * the first 128 partitions, we would forget to write the last one.
    531          */
     481         * the first 128 partitions, we would forget to write the last one.*/
    532482        memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
    533483       
    534         if (label->parts->fill > idx)
    535                 label->parts->fill = idx;
    536        
    537         /*
    538          * FIXME! HOPEFULLY FIXED.
     484        label->parts->fill -= 1;
     485       
     486        /* FIXME! HOPEFULLY FIXED.
    539487         * We cannot reduce the array so simply. We may have some partitions
    540          * there since we allow blank spots.
    541          */
     488         * there since we allow blank spots. */
    542489        gpt_part_t * p;
    543        
    544         if (label->parts->fill > GPT_MIN_PART_NUM &&
    545             label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
     490        if (label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
    546491                for (p = gpt_get_partition_at(label, label->parts->arr_size / 2);
    547492                     p < label->parts->part_array + label->parts->arr_size; ++p) {
     
    688633}
    689634
    690 /** Generate a new pseudo-random UUID
    691  * @param uuid Pointer to the UUID to overwrite.
    692  */
    693 void gpt_set_random_uuid(uint8_t * uuid)
    694 {
    695         srandom((unsigned int) uuid);
    696        
    697         unsigned int i;
    698         for (i = 0; i < 16/sizeof(long int); ++i)
    699                 ((long int *)uuid)[i] = random();
    700        
    701 }
    702 
    703 /** Get next aligned address */
    704 uint64_t gpt_get_next_aligned(uint64_t addr, unsigned int alignment)
    705 {
    706         uint64_t div = addr / alignment;
    707         return (div + 1) * alignment;
    708 }
    709 
    710 /* Internal functions follow */
     635// Internal functions follow //
    711636
    712637static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
     
    749674                return NULL;
    750675        }
    751        
     676
    752677        uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
    753678        res->part_array = malloc(size * sizeof(gpt_entry_t));
     
    757682                return NULL;
    758683        }
    759        
    760         memset(res->part_array, 0, size * sizeof(gpt_entry_t));
    761        
    762         res->fill = 0;
    763         res->arr_size = num;
     684
     685        res->fill = num;
     686        res->arr_size = size;
    764687
    765688        return res;
     
    768691static int extend_part_array(gpt_partitions_t * p)
    769692{
    770         size_t nsize = p->arr_size * 2;
     693        unsigned int nsize = p->arr_size * 2;
    771694        gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
    772695        if(tmp == NULL) {
     
    774697                return -1;
    775698        }
    776        
    777         memcpy(tmp, p->part_array, p->fill * sizeof(gpt_entry_t));
     699
     700        memcpy(tmp, p->part_array, p->fill);
    778701        free(p->part_array);
    779702        p->part_array = tmp;
     
    792715                        return ENOMEM;
    793716
    794                 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
     717                memcpy(tmp, p->part_array, p->fill < nsize ? p->fill  : nsize);
    795718                free(p->part_array);
    796719                p->part_array = tmp;
     
    801724}
    802725
    803 /*static long long nearest_larger_int(double a)
     726//FIXME: replace this with a library call, if it exists
     727static long long nearest_larger_int(double a)
    804728{
    805729        if ((long long) a == a) {
     
    808732
    809733        return ((long long) a) + 1;
    810 }*/
    811 
    812 /* Parse a byte from a string in hexadecimal
    813  * i.e., "FF" => 255
    814  */
     734}
     735
    815736static uint8_t get_byte(const char * c)
    816737{
     
    822743}
    823744
    824 static bool check_overlap(gpt_part_t * p1, gpt_part_t * p2)
     745static int check_overlap(gpt_part_t * p1, gpt_part_t * p2)
    825746{
    826747        if (gpt_get_start_lba(p1) < gpt_get_start_lba(p2) && gpt_get_end_lba(p1) <= gpt_get_start_lba(p2)) {
    827                 return false;
     748                return 0;
    828749        } else if (gpt_get_start_lba(p1) > gpt_get_start_lba(p2) && gpt_get_end_lba(p2) <= gpt_get_start_lba(p1)) {
    829                 return false;
    830         }
    831 
    832         return true;
    833 }
    834 
    835 
     750                return 0;
     751        }
     752
     753        return 1;
     754}
     755
     756
  • uspace/lib/gpt/libgpt.h

    r283ea3d r8559fa0  
    3434 */
    3535
    36 #ifndef LIBGPT_LIBGPT_H_
    37 #define LIBGPT_LIBGPT_H_
     36#ifndef __GPT_H__
     37#define __GPT_H__
    3838
    3939#define LIBGPT_NAME     "libgpt"
     
    4141#include <loc.h>
    4242#include <sys/types.h>
    43 
    44 #include "gpt.h"
    4543
    4644/** Block address of GPT header. */
     
    6159extern const uint8_t efi_signature[8];
    6260
     61typedef enum {
     62        AT_REQ_PART = 0,
     63        AT_NO_BLOCK_IO,
     64        AT_LEGACY_BOOT,
     65        AT_UNDEFINED,
     66        AT_SPECIFIC = 48
     67} GPT_ATTR;
     68
     69/** GPT header
     70 * - all in little endian.
     71 */
     72typedef struct {
     73        uint8_t  efi_signature[8];
     74        uint32_t revision;
     75        uint32_t header_size;
     76        uint32_t header_crc32;
     77        uint32_t reserved;
     78        uint64_t my_lba;
     79        uint64_t alternate_lba;
     80        uint64_t first_usable_lba;
     81        uint64_t last_usable_lba;
     82        uint8_t  disk_guid[16];
     83        uint64_t entry_lba;
     84        uint32_t fillries;
     85        uint32_t entry_size;
     86        uint32_t pe_array_crc32;
     87} __attribute__((packed)) gpt_header_t;
     88
    6389typedef struct {
    6490        /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
     
    6793} gpt_t;
    6894
     95/** GPT partition entry */
     96typedef struct {
     97        uint8_t part_type[16];
     98        uint8_t part_id[16];
     99        uint64_t start_lba;
     100        uint64_t end_lba;
     101        uint64_t attributes;
     102        uint8_t part_name[72];
     103} __attribute__((packed)) gpt_entry_t;
     104
     105
     106//typedef struct g_part {
     107        ///** Partition entry is in use **/
     108        //bool present;
     109        ///** Address of first block */
     110        //aoff64_t start_addr;
     111        ///** Number of blocks */
     112        //aoff64_t length;
     113        ///** Raw data access */
     114        //gpt_entry_t raw_data; //TODO: a pointer or just a member?
     115//}gpt_part_t;
    69116typedef gpt_entry_t gpt_part_t;
     117
    70118
    71119typedef struct gpt_parts {
     
    119167extern void            gpt_set_flag     (gpt_part_t *, GPT_ATTR, bool);
    120168
    121 extern void            gpt_set_random_uuid(uint8_t *);
    122 extern uint64_t        gpt_get_next_aligned(uint64_t, unsigned int);
    123169
    124170
    125171#define gpt_part_foreach(label, iterator) \
    126172                for(gpt_part_t * iterator = (label)->parts->part_array; \
    127                     iterator < (label)->parts->part_array + (label)->parts->arr_size; ++iterator)
     173                    iterator < (label)->parts->part_array + (label)->parts->fill; ++iterator)
    128174
    129175extern void gpt_free_gpt(gpt_t *);
  • uspace/lib/mbr/libmbr.c

    r283ea3d r8559fa0  
    4242#include <stdio.h>
    4343#include <stdlib.h>
    44 #include <str_error.h>
    4544
    4645#include "libmbr.h"
     
    5049static int decode_logical(mbr_label_t *, mbr_part_t *);
    5150static void encode_part(mbr_part_t *, pt_entry_t *, uint32_t, bool);
    52 static bool check_overlap(mbr_part_t *, mbr_part_t *);
    53 static bool check_encaps(mbr_part_t *, mbr_part_t *);
    54 static bool check_preceeds(mbr_part_t *, mbr_part_t *);
    55 static mbr_err_val mbr_add_primary(mbr_label_t *, mbr_part_t *);
    56 static mbr_err_val mbr_add_logical(mbr_label_t *, mbr_part_t *);
     51static int check_overlap(mbr_part_t *, mbr_part_t *);
     52static int check_encaps(mbr_part_t *, mbr_part_t *);
     53static int check_preceeds(mbr_part_t *, mbr_part_t *);
     54static mbr_err_val mbr_add_primary(mbr_label_t *label, mbr_part_t *p);
     55static mbr_err_val mbr_add_logical(mbr_label_t *label, mbr_part_t *p);
    5756
    5857/** Allocate and initialize mbr_label_t structure */
     
    760759/** Check whether two partitions overlap
    761760 *
    762  * @return              true/false
    763  */
    764 static bool check_overlap(mbr_part_t * p1, mbr_part_t * p2)
     761 * @return              1 for yes, 0 for no
     762 */
     763static int check_overlap(mbr_part_t * p1, mbr_part_t * p2)
    765764{
    766765        if (p1->start_addr < p2->start_addr && p1->start_addr + p1->length <= p2->start_addr) {
    767                 return false;
     766                return 0;
    768767        } else if (p1->start_addr > p2->start_addr && p2->start_addr + p2->length <= p1->start_addr) {
    769                 return false;
    770         }
    771 
    772         return true;
     768                return 0;
     769        }
     770
     771        return 1;
    773772}
    774773
    775774/** Check whether one partition encapsulates the other
    776775 *
    777  * @return              true/false
    778  */
    779 static bool check_encaps(mbr_part_t * inner, mbr_part_t * outer)
     776 * @return              1 for yes, 0 for no
     777 */
     778static int check_encaps(mbr_part_t * inner, mbr_part_t * outer)
    780779{
    781780        if (inner->start_addr <= outer->start_addr || outer->start_addr + outer->length <= inner->start_addr) {
    782                 return false;
     781                return 0;
    783782        } else if (outer->start_addr + outer->length < inner->start_addr + inner->length) {
    784                 return false;
    785         }
    786 
    787         return true;
     783                return 0;
     784        }
     785
     786        return 1;
    788787}
    789788
    790789/** Check whether one partition preceeds the other
    791790 *
    792  * @return              true/false
    793  */
    794 static bool check_preceeds(mbr_part_t * preceeder, mbr_part_t * precedee)
     791 * @return              1 for yes, 0 for no
     792 */
     793static int check_preceeds(mbr_part_t * preceeder, mbr_part_t * precedee)
    795794{
    796795        return preceeder->start_addr < precedee->start_addr;
  • uspace/lib/mbr/libmbr.h

    r283ea3d r8559fa0  
    3838
    3939#include <sys/types.h>
    40 #include "mbr.h"
    4140
    4241#define LIBMBR_NAME     "libmbr"
     42
     43#ifdef DEBUG_CONFIG
     44#include <stdio.h>
     45#include <str_error.h>
     46#define DEBUG_PRINT_0(str) \
     47        printf("%s:%d: " str, __FILE__, __LINE__)
     48#define DEBUG_PRINT_1(str, arg1) \
     49        printf("%s:%d: " str, __FILE__, __LINE__, arg1)
     50#define DEBUG_PRINT_2(str, arg1, arg2) \
     51        printf("%s:%d: " str, __FILE__, __LINE__, arg1, arg2)
     52#define DEBUG_PRINT_3(str, arg1, arg2, arg3) \
     53        printf("%s:%d: " str, __FILE__, __LINE__, arg1, arg2, arg3)
     54#else
     55#define DEBUG_PRINT_0(str)
     56#define DEBUG_PRINT_1(str, arg1)
     57#define DEBUG_PRINT_2(str, arg1, arg2)
     58#define DEBUG_PRINT_3(str, arg1, arg2, arg3)
     59#endif
     60
     61/** Number of primary partition records */
     62#define N_PRIMARY               4
     63
     64/** Boot record signature */
     65#define BR_SIGNATURE    0xAA55
     66
     67enum {
     68        /** Non-bootable */
     69        B_INACTIVE = 0x00,
     70        /** Bootable */
     71        B_ACTIVE = 0x80,
     72        /** Anything else means invalid */
     73};
    4374
    4475typedef enum {
     
    4980        ST_LOGIC = 8
    5081} MBR_FLAGS;
     82
     83enum {
     84        /** Unused partition entry */
     85        PT_UNUSED       = 0x00,
     86        /** Extended partition */
     87        PT_EXTENDED     = 0x05,
     88        /** Extended partition with LBA */
     89        PT_EXTENDED_LBA = 0x0F,
     90        /** GPT Protective partition */
     91        PT_GPT  = 0xEE,
     92};
    5193
    5294typedef enum {
     
    71113} mbr_err_val;
    72114
     115
     116/** Structure of a partition table entry */
     117typedef struct {
     118        uint8_t status;
     119        /** CHS of fist block in partition */
     120        uint8_t first_chs[3];
     121        /** Partition type */
     122        uint8_t ptype;
     123        /** CHS of last block in partition */
     124        uint8_t last_chs[3];
     125        /** LBA of first block in partition */
     126        uint32_t first_lba;
     127        /** Number of blocks in partition */
     128        uint32_t length;
     129} __attribute__((packed)) pt_entry_t;
     130
     131/** Structure of a boot-record block */
     132typedef struct {
     133        /** Area for boot code */
     134        uint8_t code_area[440];
     135        /** Optional media ID */
     136        uint32_t media_id;
     137        /** Padding */
     138        uint16_t pad0;
     139        /** Partition table entries */
     140        pt_entry_t pte[N_PRIMARY];
     141        /** Boot record block signature (@c BR_SIGNATURE) */
     142        uint16_t signature;
     143} __attribute__((packed)) br_block_t;
     144
    73145/** MBR header */
    74146typedef struct {
Note: See TracChangeset for help on using the changeset viewer.