blob: 0c51e62a87864b223fa163f9d1c6ee720ec0113b [file] [log] [blame]
Haojian Zhuang91f56462016-07-28 14:19:36 +08001/*
Douglas Raillarda8954fc2017-01-26 15:54:44 +00002 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Haojian Zhuang91f56462016-07-28 14:19:36 +08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Haojian Zhuang91f56462016-07-28 14:19:36 +08005 */
6
7#include <assert.h>
8#include <debug.h>
9#include <errno.h>
10#include <gpt.h>
11#include <string.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000012#include <utils.h>
Haojian Zhuang91f56462016-07-28 14:19:36 +080013
14static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out)
15{
Yann Gautiered041ac2018-11-09 18:21:04 +010016 uint8_t *name;
Haojian Zhuang91f56462016-07-28 14:19:36 +080017 int i;
18
Yann Gautiered041ac2018-11-09 18:21:04 +010019 assert((str_in != NULL) && (str_out != NULL));
20
21 name = (uint8_t *)str_in;
22
23 assert(name[0] != '\0');
Haojian Zhuang91f56462016-07-28 14:19:36 +080024
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
39int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry)
40{
41 int result;
42
Yann Gautiered041ac2018-11-09 18:21:04 +010043 assert((gpt_entry != NULL) && (entry != NULL));
Haojian Zhuang91f56462016-07-28 14:19:36 +080044
45 if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) {
46 return -EINVAL;
47 }
48
Douglas Raillarda8954fc2017-01-26 15:54:44 +000049 zeromem(entry, sizeof(partition_entry_t));
Haojian Zhuang91f56462016-07-28 14:19:36 +080050 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}