blob: 4577f06a20bf468e359e9eba31fe4bde9b9eb32c [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>
Haojian Zhuang91f56462016-07-28 14:19:36 +08008#include <errno.h>
Haojian Zhuang91f56462016-07-28 14:19:36 +08009#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <common/debug.h>
12#include <drivers/partition/gpt.h>
13#include <lib/utils.h>
Haojian Zhuang91f56462016-07-28 14:19:36 +080014
15static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out)
16{
Yann Gautiered041ac2018-11-09 18:21:04 +010017 uint8_t *name;
Haojian Zhuang91f56462016-07-28 14:19:36 +080018 int i;
19
Yann Gautiered041ac2018-11-09 18:21:04 +010020 assert((str_in != NULL) && (str_out != NULL));
21
22 name = (uint8_t *)str_in;
23
24 assert(name[0] != '\0');
Haojian Zhuang91f56462016-07-28 14:19:36 +080025
26 /* check whether the unicode string is valid */
27 for (i = 1; i < (EFI_NAMELEN << 1); i += 2) {
28 if (name[i] != '\0')
29 return -EINVAL;
30 }
31 /* convert the unicode string to ascii string */
32 for (i = 0; i < (EFI_NAMELEN << 1); i += 2) {
33 str_out[i >> 1] = name[i];
34 if (name[i] == '\0')
35 break;
36 }
37 return 0;
38}
39
40int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry)
41{
42 int result;
43
Yann Gautiered041ac2018-11-09 18:21:04 +010044 assert((gpt_entry != NULL) && (entry != NULL));
Haojian Zhuang91f56462016-07-28 14:19:36 +080045
46 if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) {
47 return -EINVAL;
48 }
49
Douglas Raillarda8954fc2017-01-26 15:54:44 +000050 zeromem(entry, sizeof(partition_entry_t));
Haojian Zhuang91f56462016-07-28 14:19:36 +080051 result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name);
52 if (result != 0) {
53 return result;
54 }
55 entry->start = (uint64_t)gpt_entry->first_lba * PARTITION_BLOCK_SIZE;
56 entry->length = (uint64_t)(gpt_entry->last_lba -
57 gpt_entry->first_lba + 1) *
58 PARTITION_BLOCK_SIZE;
59 return 0;
60}