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 *);
|
---|
56 | static int reduce_part_array(gpt_partitions_t *);
|
---|
57 | static long long nearest_larger_int(double a);
|
---|
58 | static uint8_t get_byte(const char *);
|
---|
59 |
|
---|
60 | /** Allocate memory for gpt label */
|
---|
61 | gpt_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 */
|
---|
75 | void 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 | }
|
---|
85 |
|
---|
86 | /** Allocate memory for gpt header */
|
---|
87 | gpt_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 */
|
---|
109 | void gpt_free_gpt(gpt_t *gpt)
|
---|
110 | {
|
---|
111 | free(gpt->header);
|
---|
112 | free(gpt);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Read GPT from specific device
|
---|
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 | */
|
---|
121 | int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
|
---|
122 | {
|
---|
123 | int rc;
|
---|
124 | size_t b_size;
|
---|
125 |
|
---|
126 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
|
---|
127 | if (rc != EOK)
|
---|
128 | return rc;
|
---|
129 |
|
---|
130 | rc = block_get_bsize(dev_handle, &b_size);
|
---|
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);
|
---|
141 | if (rc == EBADCHECKSUM || rc == EINVAL) {
|
---|
142 | aoff64_t n_blocks;
|
---|
143 | rc = block_get_nblocks(dev_handle, &n_blocks);
|
---|
144 | if (rc != EOK)
|
---|
145 | goto fail;
|
---|
146 |
|
---|
147 | rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
|
---|
148 | if (rc == EBADCHECKSUM || rc == EINVAL)
|
---|
149 | goto fail;
|
---|
150 | }
|
---|
151 |
|
---|
152 | label->device = dev_handle;
|
---|
153 | block_fini(dev_handle);
|
---|
154 | return EOK;
|
---|
155 |
|
---|
156 | fail:
|
---|
157 | block_fini(dev_handle);
|
---|
158 | gpt_free_gpt(label->gpt);
|
---|
159 | label->gpt = NULL;
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Write GPT header to device
|
---|
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 | */
|
---|
171 | int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
|
---|
172 | {
|
---|
173 | int rc;
|
---|
174 | size_t b_size;
|
---|
175 |
|
---|
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));
|
---|
179 |
|
---|
180 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
|
---|
181 | if (rc != EOK && rc != EEXIST)
|
---|
182 | return rc;
|
---|
183 |
|
---|
184 | rc = block_get_bsize(dev_handle, &b_size);
|
---|
185 | if (rc != EOK)
|
---|
186 | return rc;
|
---|
187 |
|
---|
188 | /* Write to main GPT header location */
|
---|
189 | rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
|
---|
190 | if (rc != EOK) {
|
---|
191 | block_fini(dev_handle);
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 |
|
---|
195 | aoff64_t n_blocks;
|
---|
196 | rc = block_get_nblocks(dev_handle, &n_blocks);
|
---|
197 | if (rc != EOK) {
|
---|
198 | block_fini(dev_handle);
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* Write to backup GPT header location */
|
---|
203 | //FIXME: those idiots thought it would be cool to have these fields in reverse order...
|
---|
204 | rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
|
---|
205 | block_fini(dev_handle);
|
---|
206 | if (rc != EOK)
|
---|
207 | return rc;
|
---|
208 |
|
---|
209 | return 0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Alloc partition array */
|
---|
213 | gpt_partitions_t * gpt_alloc_partitions()
|
---|
214 | {
|
---|
215 | return alloc_part_array(128);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** Parse partitions from GPT
|
---|
219 | * @param label GPT label to be parsed
|
---|
220 | *
|
---|
221 | * @return EOK on success, errorcode otherwise
|
---|
222 | */
|
---|
223 | int gpt_read_partitions(gpt_label_t *label)
|
---|
224 | {
|
---|
225 | int rc;
|
---|
226 | unsigned int i;
|
---|
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 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* We can limit comm_size like this:
|
---|
239 | * - we don't need more bytes
|
---|
240 | * - the size of GPT partition entry can be different to 128 bytes */
|
---|
241 | rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
|
---|
242 | if (rc != EOK)
|
---|
243 | goto fail;
|
---|
244 |
|
---|
245 | size_t block_size;
|
---|
246 | rc = block_get_bsize(label->device, &block_size);
|
---|
247 | if (rc != EOK)
|
---|
248 | goto fail;
|
---|
249 |
|
---|
250 | //size_t bufpos = 0;
|
---|
251 | //size_t buflen = 0;
|
---|
252 | aoff64_t pos = ent_lba * block_size;
|
---|
253 |
|
---|
254 | /* Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
|
---|
255 | * Hopefully, this does not bypass cache (no mention in libblock.c),
|
---|
256 | * and also allows us to have variable partition entry size (but we
|
---|
257 | * will always read just sizeof(gpt_entry_t) bytes - hopefully they
|
---|
258 | * don't break backward compatibility) */
|
---|
259 | for (i = 0; i < fill; ++i) {
|
---|
260 | //FIXME: this does bypass cache...
|
---|
261 | rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
|
---|
262 | //FIXME: but seqread() is just too complex...
|
---|
263 | //rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
|
---|
264 | pos += ent_size;
|
---|
265 |
|
---|
266 | if (rc != EOK)
|
---|
267 | goto fail;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* FIXME: so far my boasting about variable partition entry size
|
---|
271 | * will not work. The CRC32 checksums will be different.
|
---|
272 | * This can't be fixed easily - we'd have to run the checksum
|
---|
273 | * on all of the partition entry array.
|
---|
274 | */
|
---|
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)
|
---|
278 | {
|
---|
279 | rc = EBADCHECKSUM;
|
---|
280 | goto fail;
|
---|
281 | }
|
---|
282 |
|
---|
283 | return EOK;
|
---|
284 |
|
---|
285 | fail:
|
---|
286 | gpt_free_partitions(label->parts);
|
---|
287 | label->parts = NULL;
|
---|
288 | return rc;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** Write GPT and partitions to device
|
---|
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 | */
|
---|
297 | int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
|
---|
298 | {
|
---|
299 | int rc;
|
---|
300 | size_t b_size;
|
---|
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 |
|
---|
313 | rc = block_get_bsize(dev_handle, &b_size);
|
---|
314 | if (rc != EOK)
|
---|
315 | goto fail;
|
---|
316 |
|
---|
317 | aoff64_t n_blocks;
|
---|
318 | rc = block_get_nblocks(dev_handle, &n_blocks);
|
---|
319 | if (rc != EOK)
|
---|
320 | goto fail;
|
---|
321 |
|
---|
322 | /* Write to backup GPT partition array location */
|
---|
323 | //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 |
|
---|
336 | fail:
|
---|
337 | block_fini(dev_handle);
|
---|
338 | return rc;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /** Alloc new partition
|
---|
342 | *
|
---|
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 | */
|
---|
350 | gpt_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 | */
|
---|
372 | gpt_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 | */
|
---|
407 | gpt_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;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /** Copy partition into partition array
|
---|
418 | *
|
---|
419 | * @param parts target label
|
---|
420 | * @param partition source partition to copy
|
---|
421 | *
|
---|
422 | * @return -1 on error, 0 otherwise
|
---|
423 | *
|
---|
424 | * Note: for use with gpt_alloc_partition() only. You will get
|
---|
425 | * duplicates with gpt_get_partition().
|
---|
426 | */
|
---|
427 | int 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)
|
---|
431 | return ENOMEM;
|
---|
432 | }
|
---|
433 |
|
---|
434 | memcpy(label->parts->part_array + label->parts->fill++,
|
---|
435 | partition, sizeof(gpt_part_t));
|
---|
436 |
|
---|
437 | return EOK;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /** Remove partition from array
|
---|
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
|
---|
445 | *
|
---|
446 | * Note: even if it fails, the partition still gets removed. Only
|
---|
447 | * reducing the array failed.
|
---|
448 | */
|
---|
449 | int 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;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /** Free partition list
|
---|
474 | *
|
---|
475 | * @param parts partition list to be freed
|
---|
476 | */
|
---|
477 | void gpt_free_partitions(gpt_partitions_t * parts)
|
---|
478 | {
|
---|
479 | free(parts->part_array);
|
---|
480 | free(parts);
|
---|
481 | }
|
---|
482 |
|
---|
483 | /** Get partition type by linear search
|
---|
484 | * (hopefully this doesn't get slow)
|
---|
485 | */
|
---|
486 | size_t gpt_get_part_type(gpt_part_t * p)
|
---|
487 | {
|
---|
488 | size_t i;
|
---|
489 |
|
---|
490 | for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
|
---|
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 |
|
---|
513 | return i;
|
---|
514 | }
|
---|
515 |
|
---|
516 | /** Set partition type
|
---|
517 | * @param p partition to be set
|
---|
518 | * @param type partition type to set
|
---|
519 | * - see our fine selection at gpt_ptypes to choose from
|
---|
520 | */
|
---|
521 | void gpt_set_part_type(gpt_part_t * p, size_t type)
|
---|
522 | {
|
---|
523 | /* Beware: first 3 blocks are byteswapped! */
|
---|
524 | p->part_type[3] = gpt_ptypes[type].guid[0];
|
---|
525 | p->part_type[2] = gpt_ptypes[type].guid[1];
|
---|
526 | p->part_type[1] = gpt_ptypes[type].guid[2];
|
---|
527 | p->part_type[0] = gpt_ptypes[type].guid[3];
|
---|
528 |
|
---|
529 | p->part_type[5] = gpt_ptypes[type].guid[4];
|
---|
530 | p->part_type[4] = gpt_ptypes[type].guid[5];
|
---|
531 |
|
---|
532 | p->part_type[7] = gpt_ptypes[type].guid[6];
|
---|
533 | p->part_type[6] = gpt_ptypes[type].guid[7];
|
---|
534 |
|
---|
535 | p->part_type[8] = gpt_ptypes[type].guid[8];
|
---|
536 | p->part_type[9] = gpt_ptypes[type].guid[9];
|
---|
537 | p->part_type[10] = gpt_ptypes[type].guid[10];
|
---|
538 | p->part_type[11] = gpt_ptypes[type].guid[11];
|
---|
539 | p->part_type[12] = gpt_ptypes[type].guid[12];
|
---|
540 | p->part_type[13] = gpt_ptypes[type].guid[13];
|
---|
541 | p->part_type[14] = gpt_ptypes[type].guid[14];
|
---|
542 | p->part_type[15] = gpt_ptypes[type].guid[15];
|
---|
543 | }
|
---|
544 |
|
---|
545 | /** Get partition starting LBA */
|
---|
546 | uint64_t gpt_get_start_lba(gpt_part_t * p)
|
---|
547 | {
|
---|
548 | return uint64_t_le2host(p->start_lba);
|
---|
549 | }
|
---|
550 |
|
---|
551 | /** Set partition starting LBA */
|
---|
552 | void gpt_set_start_lba(gpt_part_t * p, uint64_t start)
|
---|
553 | {
|
---|
554 | p->start_lba = host2uint64_t_le(start);
|
---|
555 | }
|
---|
556 |
|
---|
557 | /** Get partition ending LBA */
|
---|
558 | uint64_t gpt_get_end_lba(gpt_part_t * p)
|
---|
559 | {
|
---|
560 | return uint64_t_le2host(p->end_lba);
|
---|
561 | }
|
---|
562 |
|
---|
563 | /** Set partition ending LBA */
|
---|
564 | void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
|
---|
565 | {
|
---|
566 | p->end_lba = host2uint64_t_le(end);
|
---|
567 | }
|
---|
568 |
|
---|
569 | /** Get partition name */
|
---|
570 | unsigned char * gpt_get_part_name(gpt_part_t * p)
|
---|
571 | {
|
---|
572 | return p->part_name;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /** Copy partition name */
|
---|
576 | void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
|
---|
577 | {
|
---|
578 | if (length >= 72)
|
---|
579 | length = 71;
|
---|
580 |
|
---|
581 | memcpy(p->part_name, name, length);
|
---|
582 | p->part_name[length] = '\0';
|
---|
583 | }
|
---|
584 |
|
---|
585 | /** Get partition attribute */
|
---|
586 | bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
|
---|
587 | {
|
---|
588 | return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
|
---|
589 | }
|
---|
590 |
|
---|
591 | /** Set partition attribute */
|
---|
592 | void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
|
---|
593 | {
|
---|
594 | uint64_t attr = p->attributes;
|
---|
595 |
|
---|
596 | if (value)
|
---|
597 | attr = attr | (((uint64_t) 1) << flag);
|
---|
598 | else
|
---|
599 | attr = attr ^ (attr & (((uint64_t) 1) << flag));
|
---|
600 |
|
---|
601 | p->attributes = attr;
|
---|
602 | }
|
---|
603 |
|
---|
604 | // Internal functions follow //
|
---|
605 |
|
---|
606 | static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
|
---|
607 | {
|
---|
608 | int rc;
|
---|
609 |
|
---|
610 | rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
|
---|
611 | if (rc != EOK)
|
---|
612 | return rc;
|
---|
613 |
|
---|
614 | unsigned int i;
|
---|
615 | /* Check the EFI signature */
|
---|
616 | for (i = 0; i < 8; ++i) {
|
---|
617 | if (header->efi_signature[i] != efi_signature[i])
|
---|
618 | return EINVAL;
|
---|
619 | }
|
---|
620 |
|
---|
621 | /* Check the CRC32 of the header */
|
---|
622 | uint32_t crc = header->header_crc32;
|
---|
623 | header->header_crc32 = 0;
|
---|
624 | if (crc != compute_crc32((uint8_t *) header, header->header_size))
|
---|
625 | return EBADCHECKSUM;
|
---|
626 | else
|
---|
627 | header->header_crc32 = crc;
|
---|
628 |
|
---|
629 | /* Check for zeroes in the rest of the block */
|
---|
630 | for (i = sizeof(gpt_header_t); i < b_size; ++i) {
|
---|
631 | if (((uint8_t *) header)[i] != 0)
|
---|
632 | return EINVAL;
|
---|
633 | }
|
---|
634 |
|
---|
635 | return EOK;
|
---|
636 | }
|
---|
637 |
|
---|
638 | static gpt_partitions_t * alloc_part_array(uint32_t num)
|
---|
639 | {
|
---|
640 | gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
|
---|
641 | if (res == NULL) {
|
---|
642 | errno = ENOMEM;
|
---|
643 | return NULL;
|
---|
644 | }
|
---|
645 |
|
---|
646 | uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
|
---|
647 | res->part_array = malloc(size * sizeof(gpt_entry_t));
|
---|
648 | if (res->part_array == NULL) {
|
---|
649 | free(res);
|
---|
650 | errno = ENOMEM;
|
---|
651 | return NULL;
|
---|
652 | }
|
---|
653 |
|
---|
654 | res->fill = num;
|
---|
655 | res->arr_size = size;
|
---|
656 |
|
---|
657 | return res;
|
---|
658 | }
|
---|
659 |
|
---|
660 | static int extend_part_array(gpt_partitions_t * p)
|
---|
661 | {
|
---|
662 | unsigned int nsize = p->arr_size * 2;
|
---|
663 | gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
|
---|
664 | if(tmp == NULL) {
|
---|
665 | errno = ENOMEM;
|
---|
666 | return -1;
|
---|
667 | }
|
---|
668 |
|
---|
669 | memcpy(tmp, p->part_array, p->fill);
|
---|
670 | free(p->part_array);
|
---|
671 | p->part_array = tmp;
|
---|
672 | p->arr_size = nsize;
|
---|
673 |
|
---|
674 | return 0;
|
---|
675 | }
|
---|
676 |
|
---|
677 | static int reduce_part_array(gpt_partitions_t * p)
|
---|
678 | {
|
---|
679 | if(p->arr_size > GPT_MIN_PART_NUM) {
|
---|
680 | unsigned int nsize = p->arr_size / 2;
|
---|
681 | nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
|
---|
682 | gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
|
---|
683 | if(tmp == NULL)
|
---|
684 | return ENOMEM;
|
---|
685 |
|
---|
686 | memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
|
---|
687 | free(p->part_array);
|
---|
688 | p->part_array = tmp;
|
---|
689 | p->arr_size = nsize;
|
---|
690 | }
|
---|
691 |
|
---|
692 | return 0;
|
---|
693 | }
|
---|
694 |
|
---|
695 | //FIXME: replace this with a library call, if it exists
|
---|
696 | static long long nearest_larger_int(double a)
|
---|
697 | {
|
---|
698 | if ((long long) a == a) {
|
---|
699 | return (long long) a;
|
---|
700 | }
|
---|
701 |
|
---|
702 | return ((long long) a) + 1;
|
---|
703 | }
|
---|
704 |
|
---|
705 | static 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 |
|
---|