Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 1 | /* |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 9 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <common/debug.h> |
Sughosh Ganu | 51884ac | 2021-07-02 16:33:46 +0530 | [diff] [blame] | 12 | #include <drivers/partition/efi.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | #include <drivers/partition/gpt.h> |
| 14 | #include <lib/utils.h> |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 15 | |
| 16 | static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out) |
| 17 | { |
Yann Gautier | ed041ac | 2018-11-09 18:21:04 +0100 | [diff] [blame] | 18 | uint8_t *name; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 19 | int i; |
| 20 | |
Yann Gautier | ed041ac | 2018-11-09 18:21:04 +0100 | [diff] [blame] | 21 | assert((str_in != NULL) && (str_out != NULL)); |
| 22 | |
| 23 | name = (uint8_t *)str_in; |
| 24 | |
| 25 | assert(name[0] != '\0'); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 26 | |
| 27 | /* check whether the unicode string is valid */ |
| 28 | for (i = 1; i < (EFI_NAMELEN << 1); i += 2) { |
| 29 | if (name[i] != '\0') |
| 30 | return -EINVAL; |
| 31 | } |
| 32 | /* convert the unicode string to ascii string */ |
| 33 | for (i = 0; i < (EFI_NAMELEN << 1); i += 2) { |
| 34 | str_out[i >> 1] = name[i]; |
| 35 | if (name[i] == '\0') |
| 36 | break; |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry) |
| 42 | { |
| 43 | int result; |
| 44 | |
Yann Gautier | ed041ac | 2018-11-09 18:21:04 +0100 | [diff] [blame] | 45 | assert((gpt_entry != NULL) && (entry != NULL)); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 46 | |
| 47 | if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) { |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 51 | zeromem(entry, sizeof(partition_entry_t)); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 52 | result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name); |
| 53 | if (result != 0) { |
| 54 | return result; |
| 55 | } |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 56 | entry->start = (uint64_t)gpt_entry->first_lba * |
| 57 | PLAT_PARTITION_BLOCK_SIZE; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 58 | entry->length = (uint64_t)(gpt_entry->last_lba - |
| 59 | gpt_entry->first_lba + 1) * |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 60 | PLAT_PARTITION_BLOCK_SIZE; |
Sughosh Ganu | 51884ac | 2021-07-02 16:33:46 +0530 | [diff] [blame] | 61 | guidcpy(&entry->part_guid, &gpt_entry->unique_uuid); |
| 62 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |