Changeset 1c8bfe8 in mainline for uspace/lib/gpt/libgpt.c


Ignore:
Timestamp:
2013-06-25T00:27:47Z (11 years ago)
Author:
Dominik Taborsky (AT DOT) <brembyseznamcz>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6317b33, 9256d093
Parents:
6e8e4e19 (diff), cb328ab (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

GPT updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gpt/libgpt.c

    r6e8e4e19 r1c8bfe8  
    5151#include "libgpt.h"
    5252
    53 static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t * header);
     53static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t *header);
    5454static gpt_partitions_t * alloc_part_array(uint32_t num);
    55 static int extend_part_array(gpt_partitions_t * p);
    56 static int reduce_part_array(gpt_partitions_t * p);
     55static int extend_part_array(gpt_partitions_t *);
     56static int reduce_part_array(gpt_partitions_t *);
    5757static long long nearest_larger_int(double a);
    58 static int gpt_memcmp(const void * a, const void * b, size_t len);
     58static uint8_t get_byte(const char *);
     59
     60/** Allocate memory for gpt label */
     61gpt_label_t * gpt_alloc_label(void)
     62{
     63        gpt_label_t *label = malloc(sizeof(gpt_label_t));
     64        if (label == NULL)
     65                return NULL;
     66       
     67        label->gpt = NULL;
     68        label->parts = NULL;
     69        label->device = 0;
     70       
     71        return label;
     72}
     73
     74/** Free gpt_label_t structure */
     75void gpt_free_label(gpt_label_t *label)
     76{
     77        if (label->gpt != NULL)
     78                gpt_free_gpt(label->gpt);
     79       
     80        if (label->parts != NULL)
     81                gpt_free_partitions(label->parts);
     82       
     83        free(label);
     84}
    5985
    6086/** Allocate memory for gpt header */
    61 gpt_t * gpt_alloc_gpt_header(void)
    62 {
    63         return malloc(sizeof(gpt_t));
     87gpt_t * gpt_alloc_header(size_t size)
     88{
     89        gpt_t *gpt = malloc(sizeof(gpt_t));
     90        if (gpt == NULL)
     91                return NULL;
     92       
     93        // We might need only sizeof(gpt_header_t),
     94        // but we should follow specs and have
     95        // zeroes through all the rest of the block
     96        size_t final_size = size > sizeof(gpt_header_t) ? size : sizeof(gpt_header_t);
     97        gpt->header = malloc(final_size);
     98        if (gpt->header == NULL) {
     99                free(gpt);
     100                return NULL;
     101        }
     102       
     103        memset(gpt->header, 0, final_size);
     104       
     105        return gpt;
     106}
     107
     108/** free() GPT header including gpt->header_lba */
     109void gpt_free_gpt(gpt_t *gpt)
     110{
     111        free(gpt->header);
     112        free(gpt);
    64113}
    65114
    66115/** Read GPT from specific device
    67  * @param       dev_handle      device to read GPT from
    68  *
    69  * @return                              GPT record on success, NULL on error
    70  */
    71 gpt_t * gpt_read_gpt_header(service_id_t dev_handle)
     116 * @param label        label structure to fill
     117 * @param dev_handle   device to read GPT from
     118 *
     119 * @return             EOK on success, errorcode on error
     120 */
     121int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
    72122{
    73123        int rc;
     
    76126        rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
    77127        if (rc != EOK)
    78                 return NULL;
     128                return rc;
    79129       
    80130        rc = block_get_bsize(dev_handle, &b_size);
    81         if (rc != EOK) {
    82                 errno = rc;
    83                 return NULL;
    84         }
    85        
    86         gpt_t * gpt = malloc(sizeof(gpt_t));
    87         if (gpt == NULL) {
    88                 errno = ENOMEM;
    89                 return NULL;
    90         }
    91 
    92         gpt->raw_data = malloc(b_size); // We might need only sizeof(gpt_header_t),
    93         if (gpt == NULL) {                              // but we should follow specs and have
    94                 free(gpt);                                      // zeroes through all the rest of the block
    95                 errno = ENOMEM;
    96                 return NULL;
    97         }
    98        
    99        
    100         rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, gpt->raw_data);
     131        if (rc != EOK)
     132                return rc;
     133       
     134        if (label->gpt == NULL) {
     135                label->gpt = gpt_alloc_header(b_size);
     136                if (label->gpt == NULL)
     137                        return ENOMEM;
     138        }
     139       
     140        rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, label->gpt->header);
    101141        if (rc == EBADCHECKSUM || rc == EINVAL) {
    102142                aoff64_t n_blocks;
    103143                rc = block_get_nblocks(dev_handle, &n_blocks);
    104                 if (rc != EOK) {
    105                         errno = rc;
     144                if (rc != EOK)
    106145                        goto fail;
    107                 }
    108 
    109                 rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, gpt->raw_data);
    110                 if (rc == EBADCHECKSUM || rc == EINVAL) {
    111                         errno = rc;
     146
     147                rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
     148                if (rc == EBADCHECKSUM || rc == EINVAL)
    112149                        goto fail;
    113                 }
    114         }
    115        
    116         gpt->device = dev_handle;
     150        }
     151       
     152        label->device = dev_handle;
    117153        block_fini(dev_handle);
    118         return gpt;
     154        return EOK;
    119155       
    120156fail:
    121157        block_fini(dev_handle);
    122         gpt_free_gpt(gpt);
    123         return NULL;
     158        gpt_free_gpt(label->gpt);
     159        label->gpt = NULL;
     160        return rc;
    124161}
    125162
    126163/** Write GPT header to device
    127  * @param header                GPT header to be written
    128  * @param dev_handle    device handle to write the data to
    129  *
    130  * @return                              0 on success, libblock error code otherwise
    131  *
    132  * Note: Firstly write partitions (if changed), then gpt header.
    133  */
    134 int gpt_write_gpt_header(gpt_t * gpt, service_id_t dev_handle)
     164 * @param label        GPT label header to be written
     165 * @param dev_handle   device handle to write the data to
     166 *
     167 * @return             EOK on success, libblock error code otherwise
     168 *
     169 * Note: Firstly write partitions (if modified), then gpt header.
     170 */
     171int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
    135172{
    136173        int rc;
    137174        size_t b_size;
    138175
    139         gpt->raw_data->header_crc32 = 0;
    140         gpt->raw_data->header_crc32 = compute_crc32((uint8_t *) gpt->raw_data,
    141                                         uint32_t_le2host(gpt->raw_data->header_size));
     176        label->gpt->header->header_crc32 = 0;
     177        label->gpt->header->header_crc32 = compute_crc32((uint8_t *) label->gpt->header,
     178                                        uint32_t_le2host(label->gpt->header->header_size));
    142179
    143180        rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
    144         if (rc != EOK)
     181        if (rc != EOK && rc != EEXIST)
    145182                return rc;
    146183
     
    150187
    151188        /* Write to main GPT header location */
    152         rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, gpt->raw_data);
    153         if (rc != EOK)
     189        rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
     190        if (rc != EOK) {
    154191                block_fini(dev_handle);
    155192                return rc;
     193        }
    156194
    157195        aoff64_t n_blocks;
    158196        rc = block_get_nblocks(dev_handle, &n_blocks);
    159         if (rc != EOK)
    160                 return rc;
     197        if (rc != EOK) {
     198                block_fini(dev_handle);
     199                return rc;
     200        }
    161201
    162202        /* Write to backup GPT header location */
    163203        //FIXME: those idiots thought it would be cool to have these fields in reverse order...
    164         rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, gpt->raw_data);
     204        rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
    165205        block_fini(dev_handle);
    166206        if (rc != EOK)
     
    171211
    172212/** Alloc partition array */
    173 gpt_partitions_t *      gpt_alloc_partitions()
     213gpt_partitions_t * gpt_alloc_partitions()
    174214{
    175215        return alloc_part_array(128);
     
    177217
    178218/** Parse partitions from GPT
    179  * @param gpt   GPT to be parsed
    180  *
    181  * @return              partition linked list pointer or NULL on error
    182  *                              error code is stored in errno
    183  */
    184 gpt_partitions_t * gpt_read_partitions(gpt_t * gpt)
     219 * @param label   GPT label to be parsed
     220 *
     221 * @return        EOK on success, errorcode otherwise
     222 */
     223int gpt_read_partitions(gpt_label_t *label)
    185224{
    186225        int rc;
    187226        unsigned int i;
    188         gpt_partitions_t * res;
    189         uint32_t fill = uint32_t_le2host(gpt->raw_data->fillries);
    190         uint32_t ent_size = uint32_t_le2host(gpt->raw_data->entry_size);
    191         uint64_t ent_lba = uint64_t_le2host(gpt->raw_data->entry_lba);
    192 
    193         res = alloc_part_array(fill);
    194         if (res == NULL) {
    195                 //errno = ENOMEM; // already set in alloc_part_array()
    196                 return NULL;
     227        uint32_t fill = uint32_t_le2host(label->gpt->header->fillries);
     228        uint32_t ent_size = uint32_t_le2host(label->gpt->header->entry_size);
     229        uint64_t ent_lba = uint64_t_le2host(label->gpt->header->entry_lba);
     230       
     231        if (label->parts == NULL) {
     232                label->parts = alloc_part_array(fill);
     233                if (label->parts == NULL) {
     234                        return ENOMEM;
     235                }
    197236        }
    198237
     
    200239         *  - we don't need more bytes
    201240         *  - the size of GPT partition entry can be different to 128 bytes */
    202         rc = block_init(EXCHANGE_SERIALIZE, gpt->device, sizeof(gpt_entry_t));
    203         if (rc != EOK) {
    204                 gpt_free_partitions(res);
    205                 errno = rc;
    206                 return NULL;
    207         }
     241        rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
     242        if (rc != EOK)
     243                goto fail;
    208244
    209245        size_t block_size;
    210         rc = block_get_bsize(gpt->device, &block_size);
    211         if (rc != EOK) {
    212                 gpt_free_partitions(res);
    213                 errno = rc;
    214                 return NULL;
    215         }
     246        rc = block_get_bsize(label->device, &block_size);
     247        if (rc != EOK)
     248                goto fail;
    216249
    217250        //size_t bufpos = 0;
     
    226259        for (i = 0; i < fill; ++i) {
    227260                //FIXME: this does bypass cache...
    228                 rc = block_read_bytes_direct(gpt->device, pos, sizeof(gpt_entry_t), res->part_array + i);
     261                rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
    229262                //FIXME: but seqread() is just too complex...
    230263                //rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
    231264                pos += ent_size;
    232265
    233                 if (rc != EOK) {
    234                         gpt_free_partitions(res);
    235                         errno = rc;
    236                         return NULL;
    237                 }
     266                if (rc != EOK)
     267                        goto fail;
    238268        }
    239269
     
    243273         * on all of the partition entry array.
    244274         */
    245         uint32_t crc = compute_crc32((uint8_t *) res->part_array, res->fill * sizeof(gpt_entry_t));
    246 
    247         if(uint32_t_le2host(gpt->raw_data->pe_array_crc32) != crc)
     275        uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array, label->parts->fill * sizeof(gpt_entry_t));
     276
     277        if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
    248278        {
    249                 gpt_free_partitions(res);
    250                 errno = EBADCHECKSUM;
    251                 return NULL;
    252         }
    253 
    254         return res;
     279                rc = EBADCHECKSUM;
     280                goto fail;
     281        }
     282
     283        return EOK;
     284       
     285fail:
     286        gpt_free_partitions(label->parts);
     287        label->parts = NULL;
     288        return rc;
    255289}
    256290
    257291/** Write GPT and partitions to device
    258  * @param parts                 partition list to be written
    259  * @param header                GPT header belonging to the 'parts' partitions
    260  * @param dev_handle    device to write the data to
    261  *
    262  * @return                              returns EOK on succes, specific error code otherwise
    263  */
    264 int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * gpt, service_id_t dev_handle)
     292 * @param label        label to write
     293 * @param dev_handle   device to write the data to
     294 *
     295 * @return             returns EOK on succes, errorcode otherwise
     296 */
     297int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
    265298{
    266299        int rc;
    267300        size_t b_size;
    268 
    269         gpt->raw_data->pe_array_crc32 = compute_crc32((uint8_t *) parts->part_array, parts->fill * gpt->raw_data->entry_size);
    270 
    271         rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
    272         if (rc != EOK)
    273                 return rc;
    274 
     301        uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
     302        size_t fill = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
     303       
     304        label->gpt->header->pe_array_crc32 = compute_crc32(
     305                                       (uint8_t *) label->parts->part_array,
     306                                       fill * e_size);
     307       
     308        /* comm_size of 4096 is ignored */
     309        rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
     310        if (rc != EOK && rc != EEXIST)
     311                return rc;
     312       
    275313        rc = block_get_bsize(dev_handle, &b_size);
    276314        if (rc != EOK)
    277                 return rc;
    278 
    279         /* Write to main GPT partition array location */
    280         rc = block_write_direct(dev_handle, uint64_t_le2host(gpt->raw_data->entry_lba),
    281                         nearest_larger_int((uint64_t_le2host(gpt->raw_data->entry_size) * parts->fill) / b_size),
    282                         parts->part_array);
    283         if (rc != EOK)
    284                 block_fini(dev_handle);
    285                 return rc;
    286 
     315                goto fail;
     316       
    287317        aoff64_t n_blocks;
    288318        rc = block_get_nblocks(dev_handle, &n_blocks);
    289319        if (rc != EOK)
    290                 return rc;
    291 
     320                goto fail;
     321       
    292322        /* Write to backup GPT partition array location */
    293323        //rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, header->raw_data);
     324        if (rc != EOK)
     325                goto fail;
     326       
     327        /* Write to main GPT partition array location */
     328        rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
     329                        nearest_larger_int((uint64_t_le2host(label->gpt->header->entry_size) * label->parts->fill) / b_size),
     330                        label->parts->part_array);
     331        if (rc != EOK)
     332                goto fail;
     333       
     334        return gpt_write_header(label, dev_handle);
     335       
     336fail:
    294337        block_fini(dev_handle);
    295         if (rc != EOK)
    296                 return rc;
    297 
    298 
    299         return gpt_write_gpt_header(gpt, dev_handle);
     338        return rc;
    300339}
    301340
    302341/** Alloc new partition
    303342 *
    304  * @param parts         partition table to carry new partition
    305  *
    306  * @return                      returns pointer to the new partition or NULL on ENOMEM
    307  *
    308  * Note: use either gpt_alloc_partition or gpt_add_partition. The first
    309  * returns a pointer to write your data to, the second copies the data
    310  * (and does not free the memory).
    311  */
    312 gpt_part_t * gpt_alloc_partition(gpt_partitions_t * parts)
    313 {
    314         if (parts->fill == parts->arr_size) {
    315                 if (extend_part_array(parts) == -1)
    316                         return NULL;
    317         }
    318 
    319         return parts->part_array + parts->fill++;
     343 * @return        returns pointer to the new partition or NULL
     344 *
     345 * Note: use either gpt_alloc_partition or gpt_get_partition.
     346 * This returns a memory block (zero-filled) and needs gpt_add_partition()
     347 * to be called to insert it into a partition array.
     348 * Requires you to call gpt_free_partition afterwards.
     349 */
     350gpt_part_t * gpt_alloc_partition(void)
     351{
     352        gpt_part_t *p = malloc(sizeof(gpt_part_t));
     353        if (p == NULL)
     354                return NULL;
     355       
     356        memset(p, 0, sizeof(gpt_part_t));
     357       
     358        return p;
     359}
     360
     361/** Alloc new partition already inside the label
     362 *
     363 * @param label   label to carry new partition
     364 *
     365 * @return        returns pointer to the new partition or NULL on ENOMEM
     366 *
     367 * Note: use either gpt_alloc_partition or gpt_get_partition.
     368 * This one returns a pointer to the first empty structure already
     369 * inside the array, so don't call gpt_add_partition() afterwards.
     370 * This is the one you will usually want.
     371 */
     372gpt_part_t * gpt_get_partition(gpt_label_t *label)
     373{
     374        gpt_part_t *p;
     375       
     376        /* Find the first empty entry */
     377        do {
     378                if (label->parts->fill == label->parts->arr_size) {
     379                        if (extend_part_array(label->parts) == -1)
     380                                return NULL;
     381                }
     382               
     383                p = label->parts->part_array + label->parts->fill++;
     384               
     385        } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
     386       
     387        return p;
     388}
     389
     390/** Get partition already inside the label
     391 *
     392 * @param label   label to carrying the partition
     393 * @param idx     index of the partition
     394 *
     395 * @return        returns pointer to the partition
     396 *                or NULL when out of range
     397 *
     398 * Note: For new partitions use either gpt_alloc_partition or
     399 * gpt_get_partition unless you want a partition at a specific place.
     400 * This returns a pointer to a structure already inside the array,
     401 * so don't call gpt_add_partition() afterwards.
     402 * This function is handy when you want to change already existing
     403 * partition or to simply write somewhere in the middle. This works only
     404 * for indexes smaller than either 128 or the actual number of filled
     405 * entries.
     406 */
     407gpt_part_t * gpt_get_partition_at(gpt_label_t *label, size_t idx)
     408{
     409        return NULL;
     410       
     411        if (idx >= GPT_MIN_PART_NUM && idx >= label->parts->fill)
     412                return NULL;
     413       
     414        return label->parts->part_array + idx;
    320415}
    321416
    322417/** Copy partition into partition array
    323418 *
    324  * @param parts                 target partition array
     419 * @param parts                 target label
    325420 * @param partition             source partition to copy
    326421 *
    327422 * @return                              -1 on error, 0 otherwise
    328423 *
    329  * Note: use either gpt_alloc_partition or gpt_add_partition. The first
    330  * returns a pointer to write your data to, the second copies the data
    331  * (and does not free the memory).
    332  */
    333 int gpt_add_partition(gpt_partitions_t * parts, gpt_part_t * partition)
    334 {
    335         if (parts->fill == parts->arr_size) {
    336                 if (extend_part_array(parts) == -1)
     424 * Note: for use with gpt_alloc_partition() only. You will get
     425 * duplicates with gpt_get_partition().
     426 */
     427int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
     428{
     429        if (label->parts->fill == label->parts->arr_size) {
     430                if (extend_part_array(label->parts) == -1)
    337431                        return ENOMEM;
    338432        }
    339         extend_part_array(parts);
    340         return EOK;;
     433       
     434        memcpy(label->parts->part_array + label->parts->fill++,
     435               partition, sizeof(gpt_part_t));
     436       
     437        return EOK;
    341438}
    342439
    343440/** Remove partition from array
    344  *
    345  * @param idx           index of the partition to remove
    346  *
    347  * @return                      -1 on error, 0 otherwise
     441 * @param label   label to remove from
     442 * @param idx     index of the partition to remove
     443 *
     444 * @return        EOK on success, ENOMEM on array reduction failure
    348445 *
    349446 * Note: even if it fails, the partition still gets removed. Only
    350447 * reducing the array failed.
    351448 */
    352 int gpt_remove_partition(gpt_partitions_t * parts, size_t idx)
    353 {
    354         if (idx != parts->fill - 1) {
    355                 memcpy(parts->part_array + idx, parts->part_array + parts->fill - 1, sizeof(gpt_entry_t));
    356                 parts->fill -= 1;
    357         }
    358 
    359         if (parts->fill < (parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
    360                 if (reduce_part_array(parts) == -1)
    361                         return -1;
    362         }
    363 
    364         return 0;
    365 }
    366 
    367 /** free() GPT header including gpt->header_lba */
    368 void gpt_free_gpt(gpt_t * gpt)
    369 {
    370         free(gpt->raw_data);
    371         free(gpt);
     449int gpt_remove_partition(gpt_label_t *label, size_t idx)
     450{
     451        if (idx >= label->parts->fill)
     452                return EINVAL;
     453       
     454        /* FIXME!
     455         * If we allow blank spots, we break the array. If we have more than
     456         * 128 partitions in the array and then remove something from
     457         * the first 128 partitions, we would forget to write the last one.*/
     458        memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
     459       
     460        label->parts->fill -= 1;
     461       
     462        /* FIXME!
     463         * We cannot reduce the array so simply. We may have some partitions
     464         * there since we allow blank spots.*/
     465        if (label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
     466                if (reduce_part_array(label->parts) == ENOMEM)
     467                        return ENOMEM;
     468        }
     469
     470        return EOK;
    372471}
    373472
     
    388487{
    389488        size_t i;
     489       
    390490        for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
    391                 if (gpt_memcmp(p->part_type, gpt_ptypes[i].guid, 16) == 0) {
    392                         break;
    393                 }
    394         }
     491                if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
     492                        p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
     493                        p->part_type[1] == get_byte(gpt_ptypes[i].guid +4) &&
     494                        p->part_type[0] == get_byte(gpt_ptypes[i].guid +6) &&
     495                       
     496                        p->part_type[5] == get_byte(gpt_ptypes[i].guid +8) &&
     497                        p->part_type[4] == get_byte(gpt_ptypes[i].guid +10) &&
     498                       
     499                        p->part_type[7] == get_byte(gpt_ptypes[i].guid +12) &&
     500                        p->part_type[6] == get_byte(gpt_ptypes[i].guid +14) &&
     501                       
     502                        p->part_type[8] == get_byte(gpt_ptypes[i].guid +16) &&
     503                        p->part_type[9] == get_byte(gpt_ptypes[i].guid +18) &&
     504                        p->part_type[10] == get_byte(gpt_ptypes[i].guid +20) &&
     505                        p->part_type[11] == get_byte(gpt_ptypes[i].guid +22) &&
     506                        p->part_type[12] == get_byte(gpt_ptypes[i].guid +24) &&
     507                        p->part_type[13] == get_byte(gpt_ptypes[i].guid +26) &&
     508                        p->part_type[14] == get_byte(gpt_ptypes[i].guid +28) &&
     509                        p->part_type[15] == get_byte(gpt_ptypes[i].guid +30))
     510                                break;
     511        }
     512       
    395513        return i;
    396514}
     
    449567}
    450568
    451 
     569/** Get partition name */
    452570unsigned char * gpt_get_part_name(gpt_part_t * p)
    453571{
     
    456574
    457575/** Copy partition name */
    458 void gpt_set_part_name(gpt_part_t * p, char * name[], size_t length)
     576void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
    459577{
    460578        if (length >= 72)
     
    561679        if(p->arr_size > GPT_MIN_PART_NUM) {
    562680                unsigned int nsize = p->arr_size / 2;
     681                nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
    563682                gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
    564                 if(tmp == NULL) {
    565                         errno = ENOMEM;
    566                         return -1;
    567                 }
     683                if(tmp == NULL)
     684                        return ENOMEM;
    568685
    569686                memcpy(tmp, p->part_array, p->fill < nsize ? p->fill  : nsize);
     
    586703}
    587704
    588 static int gpt_memcmp(const void * a, const void * b, size_t len)
    589 {
    590         size_t i;
    591         int diff;
    592         const unsigned char * x = a;
    593         const unsigned char * y = b;
    594 
    595         for (i = 0; i < len; i++) {
    596                 diff = (int)*(x++) - (int)*(y++);
    597                 if (diff != 0) {
    598                         return diff;
    599                 }
    600         }
    601         return 0;
    602 }
    603 
    604 
    605 
    606 
     705static uint8_t get_byte(const char * c)
     706{
     707        uint8_t val = 0;
     708        char hex[3] = {*c, *(c+1), 0};
     709       
     710        errno = str_uint8_t(hex, NULL, 16, false, &val);
     711        return val;
     712}
     713
     714
     715
     716
Note: See TracChangeset for help on using the changeset viewer.