fw_cfg.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * (C) Copyright 2015 Miao Yan <yanmiaoebst@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <errno.h>
  9. #include <malloc.h>
  10. #include <asm/io.h>
  11. #include <asm/fw_cfg.h>
  12. static bool fwcfg_present;
  13. static bool fwcfg_dma_present;
  14. /* Read configuration item using fw_cfg PIO interface */
  15. static void qemu_fwcfg_read_entry_pio(uint16_t entry,
  16. uint32_t size, void *address)
  17. {
  18. uint32_t i = 0;
  19. uint8_t *data = address;
  20. /*
  21. * writting FW_CFG_INVALID will cause read operation to resume at
  22. * last offset, otherwise read will start at offset 0
  23. */
  24. if (entry != FW_CFG_INVALID)
  25. outw(entry, FW_CONTROL_PORT);
  26. while (size--)
  27. data[i++] = inb(FW_DATA_PORT);
  28. }
  29. /* Read configuration item using fw_cfg DMA interface */
  30. static void qemu_fwcfg_read_entry_dma(uint16_t entry,
  31. uint32_t size, void *address)
  32. {
  33. struct fw_cfg_dma_access dma;
  34. dma.length = cpu_to_be32(size);
  35. dma.address = cpu_to_be64((uintptr_t)address);
  36. dma.control = cpu_to_be32(FW_CFG_DMA_READ);
  37. /*
  38. * writting FW_CFG_INVALID will cause read operation to resume at
  39. * last offset, otherwise read will start at offset 0
  40. */
  41. if (entry != FW_CFG_INVALID)
  42. dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
  43. barrier();
  44. debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
  45. address, size, be32_to_cpu(dma.control));
  46. outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
  47. while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
  48. __asm__ __volatile__ ("pause");
  49. }
  50. static bool qemu_fwcfg_present(void)
  51. {
  52. uint32_t qemu;
  53. qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
  54. return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
  55. }
  56. static bool qemu_fwcfg_dma_present(void)
  57. {
  58. uint8_t dma_enabled;
  59. qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
  60. if (dma_enabled & FW_CFG_DMA_ENABLED)
  61. return true;
  62. return false;
  63. }
  64. static void qemu_fwcfg_read_entry(uint16_t entry,
  65. uint32_t length, void *address)
  66. {
  67. if (fwcfg_dma_present)
  68. qemu_fwcfg_read_entry_dma(entry, length, address);
  69. else
  70. qemu_fwcfg_read_entry_pio(entry, length, address);
  71. }
  72. int qemu_fwcfg_online_cpus(void)
  73. {
  74. uint16_t nb_cpus;
  75. if (!fwcfg_present)
  76. return -ENODEV;
  77. qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
  78. return le16_to_cpu(nb_cpus);
  79. }
  80. /*
  81. * This function prepares kernel for zboot. It loads kernel data
  82. * to 'load_addr', initrd to 'initrd_addr' and kernel command
  83. * line using qemu fw_cfg interface.
  84. */
  85. static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
  86. {
  87. char *data_addr;
  88. uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
  89. qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
  90. qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
  91. if (setup_size == 0 || kernel_size == 0) {
  92. printf("warning: no kernel available\n");
  93. return -1;
  94. }
  95. data_addr = load_addr;
  96. qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
  97. le32_to_cpu(setup_size), data_addr);
  98. data_addr += le32_to_cpu(setup_size);
  99. qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
  100. le32_to_cpu(kernel_size), data_addr);
  101. data_addr += le32_to_cpu(kernel_size);
  102. data_addr = initrd_addr;
  103. qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
  104. if (initrd_size == 0) {
  105. printf("warning: no initrd available\n");
  106. } else {
  107. qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
  108. le32_to_cpu(initrd_size), data_addr);
  109. data_addr += le32_to_cpu(initrd_size);
  110. }
  111. qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
  112. if (cmdline_size) {
  113. qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
  114. le32_to_cpu(cmdline_size), data_addr);
  115. /*
  116. * if kernel cmdline only contains '\0', (e.g. no -append
  117. * when invoking qemu), do not update bootargs
  118. */
  119. if (*data_addr != '\0') {
  120. if (setenv("bootargs", data_addr) < 0)
  121. printf("warning: unable to change bootargs\n");
  122. }
  123. }
  124. printf("loading kernel to address %p size %x", load_addr,
  125. le32_to_cpu(kernel_size));
  126. if (initrd_size)
  127. printf(" initrd %p size %x\n",
  128. initrd_addr,
  129. le32_to_cpu(initrd_size));
  130. else
  131. printf("\n");
  132. return 0;
  133. }
  134. static int qemu_fwcfg_list_firmware(void)
  135. {
  136. int i;
  137. uint32_t count;
  138. struct fw_cfg_files *files;
  139. qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
  140. if (!count)
  141. return 0;
  142. count = be32_to_cpu(count);
  143. files = malloc(count * sizeof(struct fw_cfg_file));
  144. if (!files)
  145. return -ENOMEM;
  146. files->count = count;
  147. qemu_fwcfg_read_entry(FW_CFG_INVALID,
  148. count * sizeof(struct fw_cfg_file),
  149. files->files);
  150. for (i = 0; i < files->count; i++)
  151. printf("%-56s\n", files->files[i].name);
  152. free(files);
  153. return 0;
  154. }
  155. void qemu_fwcfg_init(void)
  156. {
  157. fwcfg_present = qemu_fwcfg_present();
  158. if (fwcfg_present)
  159. fwcfg_dma_present = qemu_fwcfg_dma_present();
  160. }
  161. static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
  162. int argc, char * const argv[])
  163. {
  164. if (qemu_fwcfg_list_firmware() < 0)
  165. return CMD_RET_FAILURE;
  166. return 0;
  167. }
  168. static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
  169. int argc, char * const argv[])
  170. {
  171. int ret = qemu_fwcfg_online_cpus();
  172. if (ret < 0) {
  173. printf("QEMU fw_cfg interface not found\n");
  174. return CMD_RET_FAILURE;
  175. }
  176. printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
  177. return 0;
  178. }
  179. static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
  180. int argc, char * const argv[])
  181. {
  182. char *env;
  183. void *load_addr;
  184. void *initrd_addr;
  185. env = getenv("loadaddr");
  186. load_addr = env ?
  187. (void *)simple_strtoul(env, NULL, 16) :
  188. (void *)CONFIG_LOADADDR;
  189. env = getenv("ramdiskaddr");
  190. initrd_addr = env ?
  191. (void *)simple_strtoul(env, NULL, 16) :
  192. (void *)CONFIG_RAMDISK_ADDR;
  193. if (argc == 2) {
  194. load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
  195. initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
  196. } else if (argc == 1) {
  197. load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
  198. }
  199. return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
  200. }
  201. static cmd_tbl_t fwcfg_commands[] = {
  202. U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
  203. U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
  204. U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
  205. };
  206. static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  207. {
  208. int ret;
  209. cmd_tbl_t *fwcfg_cmd;
  210. if (!fwcfg_present) {
  211. printf("QEMU fw_cfg interface not found\n");
  212. return CMD_RET_USAGE;
  213. }
  214. fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
  215. ARRAY_SIZE(fwcfg_commands));
  216. argc -= 2;
  217. argv += 2;
  218. if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
  219. return CMD_RET_USAGE;
  220. ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
  221. return cmd_process_error(fwcfg_cmd, ret);
  222. }
  223. U_BOOT_CMD(
  224. qfw, 4, 1, do_qemu_fw,
  225. "QEMU firmware interface",
  226. "<command>\n"
  227. " - list : print firmware(s) currently loaded\n"
  228. " - cpus : print online cpu number\n"
  229. " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
  230. )