blob: 8b1046d007076e42979b2cc127bbad071c2a8fe7 [file] [log] [blame]
Haojian Zhuang91f56462016-07-28 14:19:36 +08001/*
Lionel Debievea88ca2e2022-02-24 18:56:28 +01002 * Copyright (c) 2016-2022, 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>
Sughosh Ganu51884ac2021-07-02 16:33:46 +053012#include <drivers/partition/efi.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <drivers/partition/gpt.h>
14#include <lib/utils.h>
Haojian Zhuang91f56462016-07-28 14:19:36 +080015
16static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out)
17{
Yann Gautiered041ac2018-11-09 18:21:04 +010018 uint8_t *name;
Haojian Zhuang91f56462016-07-28 14:19:36 +080019 int i;
20
Yann Gautiered041ac2018-11-09 18:21:04 +010021 assert((str_in != NULL) && (str_out != NULL));
22
23 name = (uint8_t *)str_in;
24
25 assert(name[0] != '\0');
Haojian Zhuang91f56462016-07-28 14:19:36 +080026
27 /* check whether the unicode string is valid */
28 for (i = 1; i < (EFI_NAMELEN << 1); i += 2) {
Yann Gautier21d2f5e2022-06-21 14:34:00 +020029 if (name[i] != '\0') {
Haojian Zhuang91f56462016-07-28 14:19:36 +080030 return -EINVAL;
Yann Gautier21d2f5e2022-06-21 14:34:00 +020031 }
Haojian Zhuang91f56462016-07-28 14:19:36 +080032 }
33 /* convert the unicode string to ascii string */
34 for (i = 0; i < (EFI_NAMELEN << 1); i += 2) {
35 str_out[i >> 1] = name[i];
Yann Gautier21d2f5e2022-06-21 14:34:00 +020036 if (name[i] == '\0') {
Haojian Zhuang91f56462016-07-28 14:19:36 +080037 break;
Yann Gautier21d2f5e2022-06-21 14:34:00 +020038 }
Haojian Zhuang91f56462016-07-28 14:19:36 +080039 }
40 return 0;
41}
42
43int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry)
44{
45 int result;
46
Yann Gautiered041ac2018-11-09 18:21:04 +010047 assert((gpt_entry != NULL) && (entry != NULL));
Haojian Zhuang91f56462016-07-28 14:19:36 +080048
49 if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) {
50 return -EINVAL;
51 }
52
Douglas Raillarda8954fc2017-01-26 15:54:44 +000053 zeromem(entry, sizeof(partition_entry_t));
Haojian Zhuang91f56462016-07-28 14:19:36 +080054 result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name);
55 if (result != 0) {
56 return result;
57 }
Haojian Zhuang42a746d2019-09-14 18:01:16 +080058 entry->start = (uint64_t)gpt_entry->first_lba *
59 PLAT_PARTITION_BLOCK_SIZE;
Haojian Zhuang91f56462016-07-28 14:19:36 +080060 entry->length = (uint64_t)(gpt_entry->last_lba -
61 gpt_entry->first_lba + 1) *
Haojian Zhuang42a746d2019-09-14 18:01:16 +080062 PLAT_PARTITION_BLOCK_SIZE;
Sughosh Ganu51884ac2021-07-02 16:33:46 +053063 guidcpy(&entry->part_guid, &gpt_entry->unique_uuid);
Lionel Debievea88ca2e2022-02-24 18:56:28 +010064 guidcpy(&entry->type_guid, &gpt_entry->type_uuid);
Sughosh Ganu51884ac2021-07-02 16:33:46 +053065
Haojian Zhuang91f56462016-07-28 14:19:36 +080066 return 0;
67}