xref: /rk3399_ARM-atf/drivers/partition/gpt.c (revision f132b4a05b23916c1101add4bd6d973a99983719)
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 = (uint8_t *)str_in;
17 	int i;
18 
19 	assert((str_in != NULL) && (str_out != NULL) && (name[0] != '\0'));
20 
21 	/* check whether the unicode string is valid */
22 	for (i = 1; i < (EFI_NAMELEN << 1); i += 2) {
23 		if (name[i] != '\0')
24 			return -EINVAL;
25 	}
26 	/* convert the unicode string to ascii string */
27 	for (i = 0; i < (EFI_NAMELEN << 1); i += 2) {
28 		str_out[i >> 1] = name[i];
29 		if (name[i] == '\0')
30 			break;
31 	}
32 	return 0;
33 }
34 
35 int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry)
36 {
37 	int result;
38 
39 	assert((gpt_entry != 0) && (entry != 0));
40 
41 	if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) {
42 		return -EINVAL;
43 	}
44 
45 	zeromem(entry, sizeof(partition_entry_t));
46 	result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name);
47 	if (result != 0) {
48 		return result;
49 	}
50 	entry->start = (uint64_t)gpt_entry->first_lba * PARTITION_BLOCK_SIZE;
51 	entry->length = (uint64_t)(gpt_entry->last_lba -
52 				   gpt_entry->first_lba + 1) *
53 			PARTITION_BLOCK_SIZE;
54 	return 0;
55 }
56