1 /* 2 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <debug.h> 9 #include <errno.h> 10 #include <gpt.h> 11 #include <string.h> 12 #include <utils.h> 13 14 static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out) 15 { 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') 28 return -EINVAL; 29 } 30 /* convert the unicode string to ascii string */ 31 for (i = 0; i < (EFI_NAMELEN << 1); i += 2) { 32 str_out[i >> 1] = name[i]; 33 if (name[i] == '\0') 34 break; 35 } 36 return 0; 37 } 38 39 int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry) 40 { 41 int result; 42 43 assert((gpt_entry != NULL) && (entry != NULL)); 44 45 if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) { 46 return -EINVAL; 47 } 48 49 zeromem(entry, sizeof(partition_entry_t)); 50 result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name); 51 if (result != 0) { 52 return result; 53 } 54 entry->start = (uint64_t)gpt_entry->first_lba * PARTITION_BLOCK_SIZE; 55 entry->length = (uint64_t)(gpt_entry->last_lba - 56 gpt_entry->first_lba + 1) * 57 PARTITION_BLOCK_SIZE; 58 return 0; 59 } 60