qemu_fw_cfg.c 7.8 KB

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