fw_cfg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 <asm/io.h>
  11. #include <asm/fw_cfg.h>
  12. #include <asm/tables.h>
  13. #include <asm/e820.h>
  14. #include <linux/list.h>
  15. #include <memalign.h>
  16. static bool fwcfg_present;
  17. static bool fwcfg_dma_present;
  18. static LIST_HEAD(fw_list);
  19. /* Read configuration item using fw_cfg PIO interface */
  20. static void qemu_fwcfg_read_entry_pio(uint16_t entry,
  21. uint32_t size, void *address)
  22. {
  23. uint32_t i = 0;
  24. uint8_t *data = address;
  25. /*
  26. * writting FW_CFG_INVALID will cause read operation to resume at
  27. * last offset, otherwise read will start at offset 0
  28. */
  29. if (entry != FW_CFG_INVALID)
  30. outw(entry, FW_CONTROL_PORT);
  31. while (size--)
  32. data[i++] = inb(FW_DATA_PORT);
  33. }
  34. /* Read configuration item using fw_cfg DMA interface */
  35. static void qemu_fwcfg_read_entry_dma(uint16_t entry,
  36. uint32_t size, void *address)
  37. {
  38. struct fw_cfg_dma_access dma;
  39. dma.length = cpu_to_be32(size);
  40. dma.address = cpu_to_be64((uintptr_t)address);
  41. dma.control = cpu_to_be32(FW_CFG_DMA_READ);
  42. /*
  43. * writting FW_CFG_INVALID will cause read operation to resume at
  44. * last offset, otherwise read will start at offset 0
  45. */
  46. if (entry != FW_CFG_INVALID)
  47. dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
  48. barrier();
  49. debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
  50. address, size, be32_to_cpu(dma.control));
  51. outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
  52. while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
  53. __asm__ __volatile__ ("pause");
  54. }
  55. static bool qemu_fwcfg_present(void)
  56. {
  57. uint32_t qemu;
  58. qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
  59. return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
  60. }
  61. static bool qemu_fwcfg_dma_present(void)
  62. {
  63. uint8_t dma_enabled;
  64. qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
  65. if (dma_enabled & FW_CFG_DMA_ENABLED)
  66. return true;
  67. return false;
  68. }
  69. static void qemu_fwcfg_read_entry(uint16_t entry,
  70. uint32_t length, void *address)
  71. {
  72. if (fwcfg_dma_present)
  73. qemu_fwcfg_read_entry_dma(entry, length, address);
  74. else
  75. qemu_fwcfg_read_entry_pio(entry, length, address);
  76. }
  77. int qemu_fwcfg_online_cpus(void)
  78. {
  79. uint16_t nb_cpus;
  80. if (!fwcfg_present)
  81. return -ENODEV;
  82. qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
  83. return le16_to_cpu(nb_cpus);
  84. }
  85. /*
  86. * This function prepares kernel for zboot. It loads kernel data
  87. * to 'load_addr', initrd to 'initrd_addr' and kernel command
  88. * line using qemu fw_cfg interface.
  89. */
  90. static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
  91. {
  92. char *data_addr;
  93. uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
  94. qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
  95. qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
  96. if (setup_size == 0 || kernel_size == 0) {
  97. printf("warning: no kernel available\n");
  98. return -1;
  99. }
  100. data_addr = load_addr;
  101. qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
  102. le32_to_cpu(setup_size), data_addr);
  103. data_addr += le32_to_cpu(setup_size);
  104. qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
  105. le32_to_cpu(kernel_size), data_addr);
  106. data_addr += le32_to_cpu(kernel_size);
  107. data_addr = initrd_addr;
  108. qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
  109. if (initrd_size == 0) {
  110. printf("warning: no initrd available\n");
  111. } else {
  112. qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
  113. le32_to_cpu(initrd_size), data_addr);
  114. data_addr += le32_to_cpu(initrd_size);
  115. }
  116. qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
  117. if (cmdline_size) {
  118. qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
  119. le32_to_cpu(cmdline_size), data_addr);
  120. /*
  121. * if kernel cmdline only contains '\0', (e.g. no -append
  122. * when invoking qemu), do not update bootargs
  123. */
  124. if (*data_addr != '\0') {
  125. if (setenv("bootargs", data_addr) < 0)
  126. printf("warning: unable to change bootargs\n");
  127. }
  128. }
  129. printf("loading kernel to address %p size %x", load_addr,
  130. le32_to_cpu(kernel_size));
  131. if (initrd_size)
  132. printf(" initrd %p size %x\n",
  133. initrd_addr,
  134. le32_to_cpu(initrd_size));
  135. else
  136. printf("\n");
  137. return 0;
  138. }
  139. static int qemu_fwcfg_read_firmware_list(void)
  140. {
  141. int i;
  142. uint32_t count;
  143. struct fw_file *file;
  144. struct list_head *entry;
  145. /* don't read it twice */
  146. if (!list_empty(&fw_list))
  147. return 0;
  148. qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
  149. if (!count)
  150. return 0;
  151. count = be32_to_cpu(count);
  152. for (i = 0; i < count; i++) {
  153. file = malloc(sizeof(*file));
  154. if (!file) {
  155. printf("error: allocating resource\n");
  156. goto err;
  157. }
  158. qemu_fwcfg_read_entry(FW_CFG_INVALID,
  159. sizeof(struct fw_cfg_file), &file->cfg);
  160. file->addr = 0;
  161. list_add_tail(&file->list, &fw_list);
  162. }
  163. return 0;
  164. err:
  165. list_for_each(entry, &fw_list) {
  166. file = list_entry(entry, struct fw_file, list);
  167. free(file);
  168. }
  169. return -ENOMEM;
  170. }
  171. #ifdef CONFIG_QEMU_ACPI_TABLE
  172. static struct fw_file *qemu_fwcfg_find_file(const char *name)
  173. {
  174. struct list_head *entry;
  175. struct fw_file *file;
  176. list_for_each(entry, &fw_list) {
  177. file = list_entry(entry, struct fw_file, list);
  178. if (!strcmp(file->cfg.name, name))
  179. return file;
  180. }
  181. return NULL;
  182. }
  183. /*
  184. * This function allocates memory for ACPI tables
  185. *
  186. * @entry : BIOS linker command entry which tells where to allocate memory
  187. * (either high memory or low memory)
  188. * @addr : The address that should be used for low memory allcation. If the
  189. * memory allocation request is 'ZONE_HIGH' then this parameter will
  190. * be ignored.
  191. * @return: 0 on success, or negative value on failure
  192. */
  193. static int bios_linker_allocate(struct bios_linker_entry *entry, u32 *addr)
  194. {
  195. uint32_t size, align;
  196. struct fw_file *file;
  197. unsigned long aligned_addr;
  198. align = le32_to_cpu(entry->alloc.align);
  199. /* align must be power of 2 */
  200. if (align & (align - 1)) {
  201. printf("error: wrong alignment %u\n", align);
  202. return -EINVAL;
  203. }
  204. file = qemu_fwcfg_find_file(entry->alloc.file);
  205. if (!file) {
  206. printf("error: can't find file %s\n", entry->alloc.file);
  207. return -ENOENT;
  208. }
  209. size = be32_to_cpu(file->cfg.size);
  210. /*
  211. * ZONE_HIGH means we need to allocate from high memory, since
  212. * malloc space is already at the end of RAM, so we directly use it.
  213. * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
  214. * in which is low memory
  215. */
  216. if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
  217. aligned_addr = (unsigned long)memalign(align, size);
  218. if (!aligned_addr) {
  219. printf("error: allocating resource\n");
  220. return -ENOMEM;
  221. }
  222. } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
  223. aligned_addr = ALIGN(*addr, align);
  224. } else {
  225. printf("error: invalid allocation zone\n");
  226. return -EINVAL;
  227. }
  228. debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
  229. file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
  230. qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
  231. size, (void *)aligned_addr);
  232. file->addr = aligned_addr;
  233. /* adjust address for low memory allocation */
  234. if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
  235. *addr = (aligned_addr + size);
  236. return 0;
  237. }
  238. /*
  239. * This function patches ACPI tables previously loaded
  240. * by bios_linker_allocate()
  241. *
  242. * @entry : BIOS linker command entry which tells how to patch
  243. * ACPI tables
  244. * @return: 0 on success, or negative value on failure
  245. */
  246. static int bios_linker_add_pointer(struct bios_linker_entry *entry)
  247. {
  248. struct fw_file *dest, *src;
  249. uint32_t offset = le32_to_cpu(entry->pointer.offset);
  250. uint64_t pointer = 0;
  251. dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
  252. if (!dest || !dest->addr)
  253. return -ENOENT;
  254. src = qemu_fwcfg_find_file(entry->pointer.src_file);
  255. if (!src || !src->addr)
  256. return -ENOENT;
  257. debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
  258. dest->addr, src->addr, offset, entry->pointer.size, pointer);
  259. memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
  260. pointer = le64_to_cpu(pointer);
  261. pointer += (unsigned long)src->addr;
  262. pointer = cpu_to_le64(pointer);
  263. memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
  264. return 0;
  265. }
  266. /*
  267. * This function updates checksum fields of ACPI tables previously loaded
  268. * by bios_linker_allocate()
  269. *
  270. * @entry : BIOS linker command entry which tells where to update ACPI table
  271. * checksums
  272. * @return: 0 on success, or negative value on failure
  273. */
  274. static int bios_linker_add_checksum(struct bios_linker_entry *entry)
  275. {
  276. struct fw_file *file;
  277. uint8_t *data, cksum = 0;
  278. uint8_t *cksum_start;
  279. file = qemu_fwcfg_find_file(entry->cksum.file);
  280. if (!file || !file->addr)
  281. return -ENOENT;
  282. data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
  283. cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
  284. cksum = table_compute_checksum(cksum_start,
  285. le32_to_cpu(entry->cksum.length));
  286. *data = cksum;
  287. return 0;
  288. }
  289. unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
  290. {
  291. entries[0].addr = 0;
  292. entries[0].size = ISA_START_ADDRESS;
  293. entries[0].type = E820_RAM;
  294. entries[1].addr = ISA_START_ADDRESS;
  295. entries[1].size = ISA_END_ADDRESS - ISA_START_ADDRESS;
  296. entries[1].type = E820_RESERVED;
  297. /*
  298. * since we use memalign(malloc) to allocate high memory for
  299. * storing ACPI tables, we need to reserve them in e820 tables,
  300. * otherwise kernel will reclaim them and data will be corrupted
  301. */
  302. entries[2].addr = ISA_END_ADDRESS;
  303. entries[2].size = gd->relocaddr - TOTAL_MALLOC_LEN - ISA_END_ADDRESS;
  304. entries[2].type = E820_RAM;
  305. /* for simplicity, reserve entire malloc space */
  306. entries[3].addr = gd->relocaddr - TOTAL_MALLOC_LEN;
  307. entries[3].size = TOTAL_MALLOC_LEN;
  308. entries[3].type = E820_RESERVED;
  309. entries[4].addr = gd->relocaddr;
  310. entries[4].size = gd->ram_size - gd->relocaddr;
  311. entries[4].type = E820_RESERVED;
  312. entries[5].addr = CONFIG_PCIE_ECAM_BASE;
  313. entries[5].size = CONFIG_PCIE_ECAM_SIZE;
  314. entries[5].type = E820_RESERVED;
  315. return 6;
  316. }
  317. /* This function loads and patches ACPI tables provided by QEMU */
  318. u32 write_acpi_tables(u32 addr)
  319. {
  320. int i, ret = 0;
  321. struct fw_file *file;
  322. struct bios_linker_entry *table_loader;
  323. struct bios_linker_entry *entry;
  324. uint32_t size;
  325. struct list_head *list;
  326. /* make sure fw_list is loaded */
  327. ret = qemu_fwcfg_read_firmware_list();
  328. if (ret) {
  329. printf("error: can't read firmware file list\n");
  330. return addr;
  331. }
  332. file = qemu_fwcfg_find_file("etc/table-loader");
  333. if (!file) {
  334. printf("error: can't find etc/table-loader\n");
  335. return addr;
  336. }
  337. size = be32_to_cpu(file->cfg.size);
  338. if ((size % sizeof(*entry)) != 0) {
  339. printf("error: table-loader maybe corrupted\n");
  340. return addr;
  341. }
  342. table_loader = malloc(size);
  343. if (!table_loader) {
  344. printf("error: no memory for table-loader\n");
  345. return addr;
  346. }
  347. qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
  348. size, table_loader);
  349. for (i = 0; i < (size / sizeof(*entry)); i++) {
  350. entry = table_loader + i;
  351. switch (le32_to_cpu(entry->command)) {
  352. case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
  353. ret = bios_linker_allocate(entry, &addr);
  354. if (ret)
  355. goto out;
  356. break;
  357. case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
  358. ret = bios_linker_add_pointer(entry);
  359. if (ret)
  360. goto out;
  361. break;
  362. case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
  363. ret = bios_linker_add_checksum(entry);
  364. if (ret)
  365. goto out;
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. out:
  372. if (ret) {
  373. list_for_each(list, &fw_list) {
  374. file = list_entry(list, struct fw_file, list);
  375. if (file->addr)
  376. free((void *)file->addr);
  377. }
  378. }
  379. free(table_loader);
  380. return addr;
  381. }
  382. #endif
  383. static int qemu_fwcfg_list_firmware(void)
  384. {
  385. int ret;
  386. struct list_head *entry;
  387. struct fw_file *file;
  388. /* make sure fw_list is loaded */
  389. ret = qemu_fwcfg_read_firmware_list();
  390. if (ret)
  391. return ret;
  392. list_for_each(entry, &fw_list) {
  393. file = list_entry(entry, struct fw_file, list);
  394. printf("%-56s\n", file->cfg.name);
  395. }
  396. return 0;
  397. }
  398. void qemu_fwcfg_init(void)
  399. {
  400. fwcfg_present = qemu_fwcfg_present();
  401. if (fwcfg_present)
  402. fwcfg_dma_present = qemu_fwcfg_dma_present();
  403. }
  404. static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
  405. int argc, char * const argv[])
  406. {
  407. if (qemu_fwcfg_list_firmware() < 0)
  408. return CMD_RET_FAILURE;
  409. return 0;
  410. }
  411. static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
  412. int argc, char * const argv[])
  413. {
  414. int ret = qemu_fwcfg_online_cpus();
  415. if (ret < 0) {
  416. printf("QEMU fw_cfg interface not found\n");
  417. return CMD_RET_FAILURE;
  418. }
  419. printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
  420. return 0;
  421. }
  422. static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
  423. int argc, char * const argv[])
  424. {
  425. char *env;
  426. void *load_addr;
  427. void *initrd_addr;
  428. env = getenv("loadaddr");
  429. load_addr = env ?
  430. (void *)simple_strtoul(env, NULL, 16) :
  431. (void *)CONFIG_LOADADDR;
  432. env = getenv("ramdiskaddr");
  433. initrd_addr = env ?
  434. (void *)simple_strtoul(env, NULL, 16) :
  435. (void *)CONFIG_RAMDISK_ADDR;
  436. if (argc == 2) {
  437. load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
  438. initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
  439. } else if (argc == 1) {
  440. load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
  441. }
  442. return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
  443. }
  444. static cmd_tbl_t fwcfg_commands[] = {
  445. U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
  446. U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
  447. U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
  448. };
  449. static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  450. {
  451. int ret;
  452. cmd_tbl_t *fwcfg_cmd;
  453. if (!fwcfg_present) {
  454. printf("QEMU fw_cfg interface not found\n");
  455. return CMD_RET_USAGE;
  456. }
  457. fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
  458. ARRAY_SIZE(fwcfg_commands));
  459. argc -= 2;
  460. argv += 2;
  461. if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
  462. return CMD_RET_USAGE;
  463. ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
  464. return cmd_process_error(fwcfg_cmd, ret);
  465. }
  466. U_BOOT_CMD(
  467. qfw, 4, 1, do_qemu_fw,
  468. "QEMU firmware interface",
  469. "<command>\n"
  470. " - list : print firmware(s) currently loaded\n"
  471. " - cpus : print online cpu number\n"
  472. " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
  473. )