blob: 9cc917d33c18a6c5ac51944753534552814f7d82 [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{
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
35int 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
Douglas Raillarda8954fc2017-01-26 15:54:44 +000045 zeromem(entry, sizeof(partition_entry_t));
Haojian Zhuang91f56462016-07-28 14:19:36 +080046 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}