random.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/efi.h>
  10. #include <asm/efi.h>
  11. #include "efistub.h"
  12. struct efi_rng_protocol {
  13. efi_status_t (*get_info)(struct efi_rng_protocol *,
  14. unsigned long *, efi_guid_t *);
  15. efi_status_t (*get_rng)(struct efi_rng_protocol *,
  16. efi_guid_t *, unsigned long, u8 *out);
  17. };
  18. efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
  19. unsigned long size, u8 *out)
  20. {
  21. efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
  22. efi_status_t status;
  23. struct efi_rng_protocol *rng;
  24. status = efi_call_early(locate_protocol, &rng_proto, NULL,
  25. (void **)&rng);
  26. if (status != EFI_SUCCESS)
  27. return status;
  28. return rng->get_rng(rng, NULL, size, out);
  29. }
  30. /*
  31. * Return the number of slots covered by this entry, i.e., the number of
  32. * addresses it covers that are suitably aligned and supply enough room
  33. * for the allocation.
  34. */
  35. static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
  36. unsigned long size,
  37. unsigned long align)
  38. {
  39. u64 start, end;
  40. if (md->type != EFI_CONVENTIONAL_MEMORY)
  41. return 0;
  42. start = round_up(md->phys_addr, align);
  43. end = round_down(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - size,
  44. align);
  45. if (start > end)
  46. return 0;
  47. return (end - start + 1) / align;
  48. }
  49. /*
  50. * The UEFI memory descriptors have a virtual address field that is only used
  51. * when installing the virtual mapping using SetVirtualAddressMap(). Since it
  52. * is unused here, we can reuse it to keep track of each descriptor's slot
  53. * count.
  54. */
  55. #define MD_NUM_SLOTS(md) ((md)->virt_addr)
  56. efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
  57. unsigned long size,
  58. unsigned long align,
  59. unsigned long *addr,
  60. unsigned long random_seed)
  61. {
  62. unsigned long map_size, desc_size, total_slots = 0, target_slot;
  63. unsigned long buff_size;
  64. efi_status_t status;
  65. efi_memory_desc_t *memory_map;
  66. int map_offset;
  67. struct efi_boot_memmap map;
  68. map.map = &memory_map;
  69. map.map_size = &map_size;
  70. map.desc_size = &desc_size;
  71. map.desc_ver = NULL;
  72. map.key_ptr = NULL;
  73. map.buff_size = &buff_size;
  74. status = efi_get_memory_map(sys_table_arg, &map);
  75. if (status != EFI_SUCCESS)
  76. return status;
  77. if (align < EFI_ALLOC_ALIGN)
  78. align = EFI_ALLOC_ALIGN;
  79. /* count the suitable slots in each memory map entry */
  80. for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
  81. efi_memory_desc_t *md = (void *)memory_map + map_offset;
  82. unsigned long slots;
  83. slots = get_entry_num_slots(md, size, align);
  84. MD_NUM_SLOTS(md) = slots;
  85. total_slots += slots;
  86. }
  87. /* find a random number between 0 and total_slots */
  88. target_slot = (total_slots * (u16)random_seed) >> 16;
  89. /*
  90. * target_slot is now a value in the range [0, total_slots), and so
  91. * it corresponds with exactly one of the suitable slots we recorded
  92. * when iterating over the memory map the first time around.
  93. *
  94. * So iterate over the memory map again, subtracting the number of
  95. * slots of each entry at each iteration, until we have found the entry
  96. * that covers our chosen slot. Use the residual value of target_slot
  97. * to calculate the randomly chosen address, and allocate it directly
  98. * using EFI_ALLOCATE_ADDRESS.
  99. */
  100. for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
  101. efi_memory_desc_t *md = (void *)memory_map + map_offset;
  102. efi_physical_addr_t target;
  103. unsigned long pages;
  104. if (target_slot >= MD_NUM_SLOTS(md)) {
  105. target_slot -= MD_NUM_SLOTS(md);
  106. continue;
  107. }
  108. target = round_up(md->phys_addr, align) + target_slot * align;
  109. pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  110. status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
  111. EFI_LOADER_DATA, pages, &target);
  112. if (status == EFI_SUCCESS)
  113. *addr = target;
  114. break;
  115. }
  116. efi_call_early(free_pool, memory_map);
  117. return status;
  118. }