1 /*
2 * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include "gpt.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <string.h>
12
13 #include "efi.h"
14
unicode_to_ascii(unsigned short * str_in,unsigned char * str_out)15 static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out) {
16 uint8_t *name;
17 int i;
18
19 assert((str_in != NULL) && (str_out != NULL));
20
21 name = (uint8_t *)str_in;
22
23 assert(name[0] != '\0');
24
25 /* check whether the unicode string is valid */
26 for (i = 1; i < (EFI_NAMELEN << 1); i += 2) {
27 if (name[i] != '\0') return -EINVAL;
28 }
29 /* convert the unicode string to ascii string */
30 for (i = 0; i < (EFI_NAMELEN << 1); i += 2) {
31 str_out[i >> 1] = name[i];
32 if (name[i] == '\0') break;
33 }
34 return 0;
35 }
36
parse_gpt_entry(gpt_entry_t * gpt_entry,partition_entry_t * entry)37 int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry) {
38 int result;
39
40 assert((gpt_entry != NULL) && (entry != NULL));
41
42 if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) {
43 return -EINVAL;
44 }
45
46 memset(entry, 0, sizeof(partition_entry_t));
47 result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name);
48 if (result != 0) {
49 return result;
50 }
51 entry->start = (uint64_t)gpt_entry->first_lba * PLAT_PARTITION_BLOCK_SIZE;
52 entry->length = (uint64_t)(gpt_entry->last_lba - gpt_entry->first_lba + 1) *
53 PLAT_PARTITION_BLOCK_SIZE;
54 guidcpy(&entry->part_guid, &gpt_entry->unique_uuid);
55 guidcpy(&entry->type_guid, &gpt_entry->type_uuid);
56
57 return 0;
58 }
59