1 | /*
|
---|
2 | * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libgpt
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | /* TODO:
|
---|
36 | * This implementation only supports fixed size partition entries. Specification
|
---|
37 | * requires otherwise, though. Use void * array and casting to achieve that.
|
---|
38 | */
|
---|
39 |
|
---|
40 | #include <ipc/bd.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <block.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <assert.h>
|
---|
47 | #include <byteorder.h>
|
---|
48 | #include <checksum.h>
|
---|
49 | #include <mem.h>
|
---|
50 |
|
---|
51 | #include "libgpt.h"
|
---|
52 |
|
---|
53 | static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t * header);
|
---|
54 | static 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);
|
---|
57 | static long long nearest_larger_int(double a);
|
---|
58 | static int gpt_memcmp(const void * a, const void * b, size_t len);
|
---|
59 |
|
---|
60 | /** Read GPT from specific device
|
---|
61 | * @param dev_handle device to read GPT from
|
---|
62 | *
|
---|
63 | * @return GPT record on success, NULL on error
|
---|
64 | */
|
---|
65 | gpt_t * gpt_read_gpt_header(service_id_t dev_handle)
|
---|
66 | {
|
---|
67 | int rc;
|
---|
68 | size_t b_size;
|
---|
69 |
|
---|
70 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
|
---|
71 | if (rc != EOK)
|
---|
72 | return NULL;
|
---|
73 |
|
---|
74 | rc = block_get_bsize(dev_handle, &b_size);
|
---|
75 | if (rc != EOK) {
|
---|
76 | errno = rc;
|
---|
77 | return NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | gpt_t * gpt = malloc(sizeof(gpt_t));
|
---|
81 | if (gpt == NULL) {
|
---|
82 | errno = ENOMEM;
|
---|
83 | return NULL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | gpt->raw_data = malloc(b_size); // We might need only sizeof(gpt_header_t),
|
---|
87 | if (gpt == NULL) { // but we should follow specs and have
|
---|
88 | free(gpt); // zeroes through all the rest of the block
|
---|
89 | errno = ENOMEM;
|
---|
90 | return NULL;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, gpt->raw_data);
|
---|
95 | if (rc == EBADCHECKSUM || rc == EINVAL) {
|
---|
96 | aoff64_t n_blocks;
|
---|
97 | rc = block_get_nblocks(dev_handle, &n_blocks);
|
---|
98 | if (rc != EOK) {
|
---|
99 | errno = rc;
|
---|
100 | goto fail;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, gpt->raw_data);
|
---|
104 | if (rc == EBADCHECKSUM || rc == EINVAL) {
|
---|
105 | errno = rc;
|
---|
106 | goto fail;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | gpt->device = dev_handle;
|
---|
111 | block_fini(dev_handle);
|
---|
112 | return gpt;
|
---|
113 |
|
---|
114 | fail:
|
---|
115 | block_fini(dev_handle);
|
---|
116 | gpt_free_gpt(gpt);
|
---|
117 | return NULL;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Write GPT header to device
|
---|
121 | * @param header GPT header to be written
|
---|
122 | * @param dev_handle device handle to write the data to
|
---|
123 | *
|
---|
124 | * @return 0 on success, libblock error code otherwise
|
---|
125 | *
|
---|
126 | * Note: Firstly write partitions (if changed), then gpt header.
|
---|
127 | */
|
---|
128 | int gpt_write_gpt_header(gpt_t * gpt, service_id_t dev_handle)
|
---|
129 | {
|
---|
130 | int rc;
|
---|
131 | size_t b_size;
|
---|
132 |
|
---|
133 | gpt->raw_data->header_crc32 = 0;
|
---|
134 | gpt->raw_data->header_crc32 = compute_crc32((uint8_t *) gpt->raw_data,
|
---|
135 | uint32_t_le2host(gpt->raw_data->header_size));
|
---|
136 |
|
---|
137 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
|
---|
138 | if (rc != EOK)
|
---|
139 | return rc;
|
---|
140 |
|
---|
141 | rc = block_get_bsize(dev_handle, &b_size);
|
---|
142 | if (rc != EOK)
|
---|
143 | return rc;
|
---|
144 |
|
---|
145 | /* Write to main GPT header location */
|
---|
146 | rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, gpt->raw_data);
|
---|
147 | if (rc != EOK)
|
---|
148 | block_fini(dev_handle);
|
---|
149 | return rc;
|
---|
150 |
|
---|
151 | aoff64_t n_blocks;
|
---|
152 | rc = block_get_nblocks(dev_handle, &n_blocks);
|
---|
153 | if (rc != EOK)
|
---|
154 | return rc;
|
---|
155 |
|
---|
156 | /* Write to backup GPT header location */
|
---|
157 | //FIXME: those idiots thought it would be cool to have these fields in reverse order...
|
---|
158 | rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, gpt->raw_data);
|
---|
159 | block_fini(dev_handle);
|
---|
160 | if (rc != EOK)
|
---|
161 | return rc;
|
---|
162 |
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Parse partitions from GPT
|
---|
167 | * @param gpt GPT to be parsed
|
---|
168 | *
|
---|
169 | * @return partition linked list pointer or NULL on error
|
---|
170 | * error code is stored in errno
|
---|
171 | */
|
---|
172 | gpt_partitions_t * gpt_read_partitions(gpt_t * gpt)
|
---|
173 | {
|
---|
174 | int rc;
|
---|
175 | unsigned int i;
|
---|
176 | gpt_partitions_t * res;
|
---|
177 | uint32_t fill = uint32_t_le2host(gpt->raw_data->fillries);
|
---|
178 | uint32_t ent_size = uint32_t_le2host(gpt->raw_data->entry_size);
|
---|
179 | uint64_t ent_lba = uint64_t_le2host(gpt->raw_data->entry_lba);
|
---|
180 |
|
---|
181 | res = alloc_part_array(fill);
|
---|
182 | if (res == NULL) {
|
---|
183 | //errno = ENOMEM; // already set in alloc_part_array()
|
---|
184 | return NULL;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* We can limit comm_size like this:
|
---|
188 | * - we don't need more bytes
|
---|
189 | * - the size of GPT partition entry can be different to 128 bytes */
|
---|
190 | rc = block_init(EXCHANGE_SERIALIZE, gpt->device, sizeof(gpt_entry_t));
|
---|
191 | if (rc != EOK) {
|
---|
192 | gpt_free_partitions(res);
|
---|
193 | errno = rc;
|
---|
194 | return NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | size_t block_size;
|
---|
198 | rc = block_get_bsize(gpt->device, &block_size);
|
---|
199 | if (rc != EOK) {
|
---|
200 | gpt_free_partitions(res);
|
---|
201 | errno = rc;
|
---|
202 | return NULL;
|
---|
203 | }
|
---|
204 |
|
---|
205 | //size_t bufpos = 0;
|
---|
206 | //size_t buflen = 0;
|
---|
207 | aoff64_t pos = ent_lba * block_size;
|
---|
208 |
|
---|
209 | /* Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
|
---|
210 | * Hopefully, this does not bypass cache (no mention in libblock.c),
|
---|
211 | * and also allows us to have variable partition entry size (but we
|
---|
212 | * will always read just sizeof(gpt_entry_t) bytes - hopefully they
|
---|
213 | * don't break backward compatibility) */
|
---|
214 | for (i = 0; i < fill; ++i) {
|
---|
215 | //FIXME: this does bypass cache...
|
---|
216 | rc = block_read_bytes_direct(gpt->device, pos, sizeof(gpt_entry_t), res->part_array + i);
|
---|
217 | //FIXME: but seqread() is just too complex...
|
---|
218 | //rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
|
---|
219 | pos += ent_size;
|
---|
220 |
|
---|
221 | if (rc != EOK) {
|
---|
222 | gpt_free_partitions(res);
|
---|
223 | errno = rc;
|
---|
224 | return NULL;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | /* FIXME: so far my boasting about variable partition entry size
|
---|
229 | * will not work. The CRC32 checksums will be different.
|
---|
230 | * This can't be fixed easily - we'd have to run the checksum
|
---|
231 | * on all of the partition entry array.
|
---|
232 | */
|
---|
233 | uint32_t crc = compute_crc32((uint8_t *) res->part_array, res->fill * sizeof(gpt_entry_t));
|
---|
234 |
|
---|
235 | if(uint32_t_le2host(gpt->raw_data->pe_array_crc32) != crc)
|
---|
236 | {
|
---|
237 | gpt_free_partitions(res);
|
---|
238 | errno = EBADCHECKSUM;
|
---|
239 | return NULL;
|
---|
240 | }
|
---|
241 |
|
---|
242 | return res;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** Write GPT and partitions to device
|
---|
246 | * @param parts partition list to be written
|
---|
247 | * @param header GPT header belonging to the 'parts' partitions
|
---|
248 | * @param dev_handle device to write the data to
|
---|
249 | *
|
---|
250 | * @return returns EOK on succes, specific error code otherwise
|
---|
251 | */
|
---|
252 | int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * gpt, service_id_t dev_handle)
|
---|
253 | {
|
---|
254 | int rc;
|
---|
255 | size_t b_size;
|
---|
256 |
|
---|
257 | gpt->raw_data->pe_array_crc32 = compute_crc32((uint8_t *) parts->part_array, parts->fill * gpt->raw_data->entry_size);
|
---|
258 |
|
---|
259 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
|
---|
260 | if (rc != EOK)
|
---|
261 | return rc;
|
---|
262 |
|
---|
263 | rc = block_get_bsize(dev_handle, &b_size);
|
---|
264 | if (rc != EOK)
|
---|
265 | return rc;
|
---|
266 |
|
---|
267 | /* Write to main GPT partition array location */
|
---|
268 | rc = block_write_direct(dev_handle, uint64_t_le2host(gpt->raw_data->entry_lba),
|
---|
269 | nearest_larger_int((uint64_t_le2host(gpt->raw_data->entry_size) * parts->fill) / b_size),
|
---|
270 | parts->part_array);
|
---|
271 | if (rc != EOK)
|
---|
272 | block_fini(dev_handle);
|
---|
273 | return rc;
|
---|
274 |
|
---|
275 | aoff64_t n_blocks;
|
---|
276 | rc = block_get_nblocks(dev_handle, &n_blocks);
|
---|
277 | if (rc != EOK)
|
---|
278 | return rc;
|
---|
279 |
|
---|
280 | /* Write to backup GPT partition array location */
|
---|
281 | //rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, header->raw_data);
|
---|
282 | block_fini(dev_handle);
|
---|
283 | if (rc != EOK)
|
---|
284 | return rc;
|
---|
285 |
|
---|
286 |
|
---|
287 | return gpt_write_gpt_header(gpt, dev_handle);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /** Alloc new partition
|
---|
291 | *
|
---|
292 | * @param parts partition table to carry new partition
|
---|
293 | *
|
---|
294 | * @return returns pointer to the new partition or NULL on ENOMEM
|
---|
295 | *
|
---|
296 | * Note: use either gpt_alloc_partition or gpt_add_partition. The first
|
---|
297 | * returns a pointer to write your data to, the second copies the data
|
---|
298 | * (and does not free the memory).
|
---|
299 | */
|
---|
300 | gpt_part_t * gpt_alloc_partition(gpt_partitions_t * parts)
|
---|
301 | {
|
---|
302 | if (parts->fill == parts->arr_size) {
|
---|
303 | if (extend_part_array(parts) == -1)
|
---|
304 | return NULL;
|
---|
305 | }
|
---|
306 |
|
---|
307 | return parts->part_array + parts->fill++;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Copy partition into partition array
|
---|
311 | *
|
---|
312 | * @param parts target partition array
|
---|
313 | * @param partition source partition to copy
|
---|
314 | *
|
---|
315 | * @return -1 on error, 0 otherwise
|
---|
316 | *
|
---|
317 | * Note: use either gpt_alloc_partition or gpt_add_partition. The first
|
---|
318 | * returns a pointer to write your data to, the second copies the data
|
---|
319 | * (and does not free the memory).
|
---|
320 | */
|
---|
321 | int gpt_add_partition(gpt_partitions_t * parts, gpt_part_t * partition)
|
---|
322 | {
|
---|
323 | if (parts->fill == parts->arr_size) {
|
---|
324 | if (extend_part_array(parts) == -1)
|
---|
325 | return ENOMEM;
|
---|
326 | }
|
---|
327 | extend_part_array(parts);
|
---|
328 | return EOK;;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** Remove partition from array
|
---|
332 | *
|
---|
333 | * @param idx index of the partition to remove
|
---|
334 | *
|
---|
335 | * @return -1 on error, 0 otherwise
|
---|
336 | *
|
---|
337 | * Note: even if it fails, the partition still gets removed. Only
|
---|
338 | * reducing the array failed.
|
---|
339 | */
|
---|
340 | int gpt_remove_partition(gpt_partitions_t * parts, size_t idx)
|
---|
341 | {
|
---|
342 | if (idx != parts->fill - 1) {
|
---|
343 | memcpy(parts->part_array + idx, parts->part_array + parts->fill - 1, sizeof(gpt_entry_t));
|
---|
344 | parts->fill -= 1;
|
---|
345 | }
|
---|
346 |
|
---|
347 | if (parts->fill < (parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
|
---|
348 | if (reduce_part_array(parts) == -1)
|
---|
349 | return -1;
|
---|
350 | }
|
---|
351 |
|
---|
352 | return 0;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /** free() GPT header including gpt->header_lba */
|
---|
356 | void gpt_free_gpt(gpt_t * gpt)
|
---|
357 | {
|
---|
358 | free(gpt->raw_data);
|
---|
359 | free(gpt);
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** Free partition list
|
---|
363 | *
|
---|
364 | * @param parts partition list to be freed
|
---|
365 | */
|
---|
366 | void gpt_free_partitions(gpt_partitions_t * parts)
|
---|
367 | {
|
---|
368 | free(parts->part_array);
|
---|
369 | free(parts);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** Get partition type by linear search
|
---|
373 | * (hopefully this doesn't get slow)
|
---|
374 | */
|
---|
375 | size_t gpt_get_part_type(gpt_part_t * p)
|
---|
376 | {
|
---|
377 | size_t i;
|
---|
378 | for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
|
---|
379 | if (gpt_memcmp(p->part_type, gpt_ptypes[i].guid, 16) == 0) {
|
---|
380 | break;
|
---|
381 | }
|
---|
382 | }
|
---|
383 | return i;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /** Set partition type
|
---|
387 | * @param p partition to be set
|
---|
388 | * @param type partition type to set
|
---|
389 | * - see our fine selection at gpt_ptypes to choose from
|
---|
390 | */
|
---|
391 | void gpt_set_part_type(gpt_part_t * p, size_t type)
|
---|
392 | {
|
---|
393 | /* Beware: first 3 blocks are byteswapped! */
|
---|
394 | p->part_type[3] = gpt_ptypes[type].guid[0];
|
---|
395 | p->part_type[2] = gpt_ptypes[type].guid[1];
|
---|
396 | p->part_type[1] = gpt_ptypes[type].guid[2];
|
---|
397 | p->part_type[0] = gpt_ptypes[type].guid[3];
|
---|
398 |
|
---|
399 | p->part_type[5] = gpt_ptypes[type].guid[4];
|
---|
400 | p->part_type[4] = gpt_ptypes[type].guid[5];
|
---|
401 |
|
---|
402 | p->part_type[7] = gpt_ptypes[type].guid[6];
|
---|
403 | p->part_type[6] = gpt_ptypes[type].guid[7];
|
---|
404 |
|
---|
405 | p->part_type[8] = gpt_ptypes[type].guid[8];
|
---|
406 | p->part_type[9] = gpt_ptypes[type].guid[9];
|
---|
407 | p->part_type[10] = gpt_ptypes[type].guid[10];
|
---|
408 | p->part_type[11] = gpt_ptypes[type].guid[11];
|
---|
409 | p->part_type[12] = gpt_ptypes[type].guid[12];
|
---|
410 | p->part_type[13] = gpt_ptypes[type].guid[13];
|
---|
411 | p->part_type[14] = gpt_ptypes[type].guid[14];
|
---|
412 | p->part_type[15] = gpt_ptypes[type].guid[15];
|
---|
413 | }
|
---|
414 |
|
---|
415 | /** Get partition starting LBA */
|
---|
416 | uint64_t gpt_get_start_lba(gpt_part_t * p)
|
---|
417 | {
|
---|
418 | return uint64_t_le2host(p->start_lba);
|
---|
419 | }
|
---|
420 |
|
---|
421 | /** Set partition starting LBA */
|
---|
422 | void gpt_set_start_lba(gpt_part_t * p, uint64_t start)
|
---|
423 | {
|
---|
424 | p->start_lba = host2uint64_t_le(start);
|
---|
425 | }
|
---|
426 |
|
---|
427 | /** Get partition ending LBA */
|
---|
428 | uint64_t gpt_get_end_lba(gpt_part_t * p)
|
---|
429 | {
|
---|
430 | return uint64_t_le2host(p->end_lba);
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Set partition ending LBA */
|
---|
434 | void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
|
---|
435 | {
|
---|
436 | p->end_lba = host2uint64_t_le(end);
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | unsigned char * gpt_get_part_name(gpt_part_t * p)
|
---|
441 | {
|
---|
442 | return p->part_name;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** Copy partition name */
|
---|
446 | void gpt_set_part_name(gpt_part_t * p, char * name[], size_t length)
|
---|
447 | {
|
---|
448 | if (length >= 72)
|
---|
449 | length = 71;
|
---|
450 |
|
---|
451 | memcpy(p->part_name, name, length);
|
---|
452 | p->part_name[length] = '\0';
|
---|
453 | }
|
---|
454 |
|
---|
455 | /** Get partition attribute */
|
---|
456 | bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
|
---|
457 | {
|
---|
458 | return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /** Set partition attribute */
|
---|
462 | void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
|
---|
463 | {
|
---|
464 | uint64_t attr = p->attributes;
|
---|
465 |
|
---|
466 | if (value)
|
---|
467 | attr = attr | (((uint64_t) 1) << flag);
|
---|
468 | else
|
---|
469 | attr = attr ^ (attr & (((uint64_t) 1) << flag));
|
---|
470 |
|
---|
471 | p->attributes = attr;
|
---|
472 | }
|
---|
473 |
|
---|
474 | // Internal functions follow //
|
---|
475 |
|
---|
476 | static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
|
---|
477 | {
|
---|
478 | int rc;
|
---|
479 |
|
---|
480 | rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
|
---|
481 | if (rc != EOK)
|
---|
482 | return rc;
|
---|
483 |
|
---|
484 | unsigned int i;
|
---|
485 | /* Check the EFI signature */
|
---|
486 | for (i = 0; i < 8; ++i) {
|
---|
487 | if (header->efi_signature[i] != efi_signature[i])
|
---|
488 | return EINVAL;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /* Check the CRC32 of the header */
|
---|
492 | uint32_t crc = header->header_crc32;
|
---|
493 | header->header_crc32 = 0;
|
---|
494 | if (crc != compute_crc32((uint8_t *) header, header->header_size))
|
---|
495 | return EBADCHECKSUM;
|
---|
496 | else
|
---|
497 | header->header_crc32 = crc;
|
---|
498 |
|
---|
499 | /* Check for zeroes in the rest of the block */
|
---|
500 | for (i = sizeof(gpt_header_t); i < b_size; ++i) {
|
---|
501 | if (((uint8_t *) header)[i] != 0)
|
---|
502 | return EINVAL;
|
---|
503 | }
|
---|
504 |
|
---|
505 | return EOK;
|
---|
506 | }
|
---|
507 |
|
---|
508 | static gpt_partitions_t * alloc_part_array(uint32_t num)
|
---|
509 | {
|
---|
510 | gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
|
---|
511 | if (res == NULL) {
|
---|
512 | errno = ENOMEM;
|
---|
513 | return NULL;
|
---|
514 | }
|
---|
515 |
|
---|
516 | uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
|
---|
517 | res->part_array = malloc(size * sizeof(gpt_entry_t));
|
---|
518 | if (res->part_array == NULL) {
|
---|
519 | free(res);
|
---|
520 | errno = ENOMEM;
|
---|
521 | return NULL;
|
---|
522 | }
|
---|
523 |
|
---|
524 | res->fill = num;
|
---|
525 | res->arr_size = size;
|
---|
526 |
|
---|
527 | return res;
|
---|
528 | }
|
---|
529 |
|
---|
530 | static int extend_part_array(gpt_partitions_t * p)
|
---|
531 | {
|
---|
532 | unsigned int nsize = p->arr_size * 2;
|
---|
533 | gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
|
---|
534 | if(tmp == NULL) {
|
---|
535 | errno = ENOMEM;
|
---|
536 | return -1;
|
---|
537 | }
|
---|
538 |
|
---|
539 | memcpy(tmp, p->part_array, p->fill);
|
---|
540 | free(p->part_array);
|
---|
541 | p->part_array = tmp;
|
---|
542 | p->arr_size = nsize;
|
---|
543 |
|
---|
544 | return 0;
|
---|
545 | }
|
---|
546 |
|
---|
547 | static int reduce_part_array(gpt_partitions_t * p)
|
---|
548 | {
|
---|
549 | if(p->arr_size > GPT_MIN_PART_NUM) {
|
---|
550 | unsigned int nsize = p->arr_size / 2;
|
---|
551 | gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
|
---|
552 | if(tmp == NULL) {
|
---|
553 | errno = ENOMEM;
|
---|
554 | return -1;
|
---|
555 | }
|
---|
556 |
|
---|
557 | memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
|
---|
558 | free(p->part_array);
|
---|
559 | p->part_array = tmp;
|
---|
560 | p->arr_size = nsize;
|
---|
561 | }
|
---|
562 |
|
---|
563 | return 0;
|
---|
564 | }
|
---|
565 |
|
---|
566 | //FIXME: replace this with a library call, if it exists
|
---|
567 | static long long nearest_larger_int(double a)
|
---|
568 | {
|
---|
569 | if ((long long) a == a) {
|
---|
570 | return (long long) a;
|
---|
571 | }
|
---|
572 |
|
---|
573 | return ((long long) a) + 1;
|
---|
574 | }
|
---|
575 |
|
---|
576 | static int gpt_memcmp(const void * a, const void * b, size_t len)
|
---|
577 | {
|
---|
578 | size_t i;
|
---|
579 | int diff;
|
---|
580 | const unsigned char * x = a;
|
---|
581 | const unsigned char * y = b;
|
---|
582 |
|
---|
583 | for (i = 0; i < len; i++) {
|
---|
584 | diff = (int)*(x++) - (int)*(y++);
|
---|
585 | if (diff != 0) {
|
---|
586 | return diff;
|
---|
587 | }
|
---|
588 | }
|
---|
589 | return 0;
|
---|
590 | }
|
---|
591 |
|
---|
592 |
|
---|
593 |
|
---|
594 |
|
---|