random.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <linux/log2.h>
  11. #include <asm/efi.h>
  12. #include "efistub.h"
  13. struct efi_rng_protocol {
  14. efi_status_t (*get_info)(struct efi_rng_protocol *,
  15. unsigned long *, efi_guid_t *);
  16. efi_status_t (*get_rng)(struct efi_rng_protocol *,
  17. efi_guid_t *, unsigned long, u8 *out);
  18. };
  19. efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
  20. unsigned long size, u8 *out)
  21. {
  22. efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
  23. efi_status_t status;
  24. struct efi_rng_protocol *rng;
  25. status = efi_call_early(locate_protocol, &rng_proto, NULL,
  26. (void **)&rng);
  27. if (status != EFI_SUCCESS)
  28. return status;
  29. return rng->get_rng(rng, NULL, size, out);
  30. }
  31. /*
  32. * Return the number of slots covered by this entry, i.e., the number of
  33. * addresses it covers that are suitably aligned and supply enough room
  34. * for the allocation.
  35. */
  36. static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
  37. unsigned long size,
  38. unsigned long align_shift)
  39. {
  40. unsigned long align = 1UL << align_shift;
  41. u64 first_slot, last_slot, region_end;
  42. if (md->type != EFI_CONVENTIONAL_MEMORY)
  43. return 0;
  44. region_end = min((u64)ULONG_MAX, md->phys_addr + md->num_pages*EFI_PAGE_SIZE - 1);
  45. first_slot = round_up(md->phys_addr, align);
  46. last_slot = round_down(region_end - size + 1, align);
  47. if (first_slot > last_slot)
  48. return 0;
  49. return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
  50. }
  51. /*
  52. * The UEFI memory descriptors have a virtual address field that is only used
  53. * when installing the virtual mapping using SetVirtualAddressMap(). Since it
  54. * is unused here, we can reuse it to keep track of each descriptor's slot
  55. * count.
  56. */
  57. #define MD_NUM_SLOTS(md) ((md)->virt_addr)
  58. efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
  59. unsigned long size,
  60. unsigned long align,
  61. unsigned long *addr,
  62. unsigned long random_seed)
  63. {
  64. unsigned long map_size, desc_size, total_slots = 0, target_slot;
  65. unsigned long buff_size;
  66. efi_status_t status;
  67. efi_memory_desc_t *memory_map;
  68. int map_offset;
  69. struct efi_boot_memmap map;
  70. map.map = &memory_map;
  71. map.map_size = &map_size;
  72. map.desc_size = &desc_size;
  73. map.desc_ver = NULL;
  74. map.key_ptr = NULL;
  75. map.buff_size = &buff_size;
  76. status = efi_get_memory_map(sys_table_arg, &map);
  77. if (status != EFI_SUCCESS)
  78. return status;
  79. if (align < EFI_ALLOC_ALIGN)
  80. align = EFI_ALLOC_ALIGN;
  81. /* count the suitable slots in each memory map entry */
  82. for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
  83. efi_memory_desc_t *md = (void *)memory_map + map_offset;
  84. unsigned long slots;
  85. slots = get_entry_num_slots(md, size, ilog2(align));
  86. MD_NUM_SLOTS(md) = slots;
  87. total_slots += slots;
  88. }
  89. /* find a random number between 0 and total_slots */
  90. target_slot = (total_slots * (u16)random_seed) >> 16;
  91. /*
  92. * target_slot is now a value in the range [0, total_slots), and so
  93. * it corresponds with exactly one of the suitable slots we recorded
  94. * when iterating over the memory map the first time around.
  95. *
  96. * So iterate over the memory map again, subtracting the number of
  97. * slots of each entry at each iteration, until we have found the entry
  98. * that covers our chosen slot. Use the residual value of target_slot
  99. * to calculate the randomly chosen address, and allocate it directly
  100. * using EFI_ALLOCATE_ADDRESS.
  101. */
  102. for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
  103. efi_memory_desc_t *md = (void *)memory_map + map_offset;
  104. efi_physical_addr_t target;
  105. unsigned long pages;
  106. if (target_slot >= MD_NUM_SLOTS(md)) {
  107. target_slot -= MD_NUM_SLOTS(md);
  108. continue;
  109. }
  110. target = round_up(md->phys_addr, align) + target_slot * align;
  111. pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  112. status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
  113. EFI_LOADER_DATA, pages, &target);
  114. if (status == EFI_SUCCESS)
  115. *addr = target;
  116. break;
  117. }
  118. efi_call_early(free_pool, memory_map);
  119. return status;
  120. }
  121. efi_status_t efi_random_get_seed(efi_system_table_t *sys_table_arg)
  122. {
  123. efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
  124. efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
  125. efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
  126. struct efi_rng_protocol *rng;
  127. struct linux_efi_random_seed *seed;
  128. efi_status_t status;
  129. status = efi_call_early(locate_protocol, &rng_proto, NULL,
  130. (void **)&rng);
  131. if (status != EFI_SUCCESS)
  132. return status;
  133. status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
  134. sizeof(*seed) + EFI_RANDOM_SEED_SIZE,
  135. (void **)&seed);
  136. if (status != EFI_SUCCESS)
  137. return status;
  138. status = rng->get_rng(rng, &rng_algo_raw, EFI_RANDOM_SEED_SIZE,
  139. seed->bits);
  140. if (status == EFI_UNSUPPORTED)
  141. /*
  142. * Use whatever algorithm we have available if the raw algorithm
  143. * is not implemented.
  144. */
  145. status = rng->get_rng(rng, NULL, EFI_RANDOM_SEED_SIZE,
  146. seed->bits);
  147. if (status != EFI_SUCCESS)
  148. goto err_freepool;
  149. seed->size = EFI_RANDOM_SEED_SIZE;
  150. status = efi_call_early(install_configuration_table, &rng_table_guid,
  151. seed);
  152. if (status != EFI_SUCCESS)
  153. goto err_freepool;
  154. return EFI_SUCCESS;
  155. err_freepool:
  156. efi_call_early(free_pool, seed);
  157. return status;
  158. }