Changeset 2b55edb in mainline


Ignore:
Timestamp:
2013-07-30T08:00:44Z (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:
493b881
Parents:
e3bc355
Message:

libgpt checks boundaries

Location:
uspace
Files:
2 edited

Legend:

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

    re3bc355 r2b55edb  
    184184                sa = gpt_get_next_aligned(sa, alignment);
    185185       
    186         printf("Set end addres (number): ");
     186        printf("Set end address (number): ");
    187187        ea = get_input_uint64(in);
    188188       
  • uspace/lib/gpt/libgpt.c

    re3bc355 r2b55edb  
    5858static uint8_t get_byte(const char *);
    5959static bool check_overlap(gpt_part_t *, gpt_part_t *);
     60static bool check_encaps(gpt_part_t *, uint64_t, uint64_t);
    6061
    6162/** Allocate memory for gpt label */
     
    308309                        goto fini_fail;
    309310        }
    310 
    311         /*
    312          * FIXME: so far my boasting about variable partition entry size
    313          * will not work. The CRC32 checksums will be different.
    314          * This can't be fixed easily - we'd have to run the checksum
    315          * on all of the partition entry array.
    316          */
     311       
    317312        uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array,
    318                            fillries * sizeof(gpt_entry_t));
     313                           fillries * ent_size);
    319314
    320315        if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
     
    350345        size_t fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
    351346       
    352         label->gpt->header->fillries = host2uint32_t_le(fillries);
    353         label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
    354                                        (uint8_t *) label->parts->part_array,
    355                                        fillries * e_size));
     347        if (e_size != sizeof(gpt_entry_t))
     348                return ENOTSUP;
    356349       
    357350        /* comm_size of 4096 is ignored */
     
    369362                goto fail;
    370363       
     364        label->gpt->header->fillries = host2uint32_t_le(fillries);
    371365        uint64_t arr_blocks = (fillries * sizeof(gpt_entry_t)) / b_size;
    372366        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);
     367        uint64_t first_lba = n_blocks - arr_blocks - 2;
     368        label->gpt->header->last_usable_lba = host2uint64_t_le(first_lba);
     369       
     370        /* Perform checks */
     371        gpt_part_foreach(label, p) {
     372                if (gpt_get_part_type(p) == GPT_PTE_UNUSED)
     373                        continue;
     374               
     375                if (!check_encaps(p, n_blocks, first_lba)) {
     376                        rc = ERANGE;
     377                        goto fail;
     378                }
     379               
     380                gpt_part_foreach(label, q) {
     381                        if (p == q)
     382                                continue;
     383                       
     384                        if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
     385                                if (check_overlap(p, q)) {
     386                                        rc = ERANGE;
     387                                        goto fail;
     388                                }
     389                        }
     390                }
     391        }
     392       
     393        label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
     394                                       (uint8_t *) label->parts->part_array,
     395                                       fillries * e_size));
    374396       
    375397       
     
    483505int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
    484506{
    485         /* FIXME: Check dimensions! */
    486         gpt_part_foreach(label, p) {
    487                 if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
    488                         if (check_overlap(partition, p))
    489                                 return EINVAL;
    490                 }
    491         }
    492        
    493507        gpt_part_t *p;
    494508        /* Find the first empty entry */
     
    833847}
    834848
    835 
     849static bool check_encaps(gpt_part_t *p, uint64_t n_blocks, uint64_t first_lba)
     850{
     851        uint64_t start = uint64_t_le2host(p->start_lba);
     852        uint64_t end = uint64_t_le2host(p->end_lba);
     853       
     854        if (start >= first_lba && end < n_blocks - first_lba)
     855                return true;
     856       
     857        return false;
     858}
Note: See TracChangeset for help on using the changeset viewer.