gpt.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include <drivers/partition/efi.h>
  11. #include <drivers/partition/gpt.h>
  12. #include <lib/utils.h>
  13. static int unicode_to_ascii(unsigned short *str_in, unsigned char *str_out)
  14. {
  15. uint8_t *name;
  16. int i;
  17. assert((str_in != NULL) && (str_out != NULL));
  18. name = (uint8_t *)str_in;
  19. assert(name[0] != '\0');
  20. /* check whether the unicode string is valid */
  21. for (i = 1; i < (EFI_NAMELEN << 1); i += 2) {
  22. if (name[i] != '\0') {
  23. return -EINVAL;
  24. }
  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. }
  33. return 0;
  34. }
  35. int parse_gpt_entry(gpt_entry_t *gpt_entry, partition_entry_t *entry)
  36. {
  37. int result;
  38. assert((gpt_entry != NULL) && (entry != NULL));
  39. if ((gpt_entry->first_lba == 0) && (gpt_entry->last_lba == 0)) {
  40. return -EINVAL;
  41. }
  42. zeromem(entry, sizeof(partition_entry_t));
  43. result = unicode_to_ascii(gpt_entry->name, (uint8_t *)entry->name);
  44. if (result != 0) {
  45. return result;
  46. }
  47. entry->start = (uint64_t)gpt_entry->first_lba *
  48. PLAT_PARTITION_BLOCK_SIZE;
  49. entry->length = (uint64_t)(gpt_entry->last_lba -
  50. gpt_entry->first_lba + 1) *
  51. PLAT_PARTITION_BLOCK_SIZE;
  52. guidcpy(&entry->part_guid, &gpt_entry->unique_uuid);
  53. guidcpy(&entry->type_guid, &gpt_entry->type_uuid);
  54. return 0;
  55. }