boot.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * AVR loader helpers
  3. *
  4. * Copyright (c) 2019-2020 Philippe Mathieu-Daudé
  5. *
  6. * This work is licensed under the terms of the GNU GPLv2 or later.
  7. * See the COPYING file in the top-level directory.
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/datadir.h"
  12. #include "hw/loader.h"
  13. #include "elf.h"
  14. #include "boot.h"
  15. #include "qemu/error-report.h"
  16. static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags)
  17. {
  18. switch (flags & EF_AVR_MACH) {
  19. case bfd_mach_avr1:
  20. return AVR_CPU_TYPE_NAME("avr1");
  21. case bfd_mach_avr2:
  22. return AVR_CPU_TYPE_NAME("avr2");
  23. case bfd_mach_avr25:
  24. return AVR_CPU_TYPE_NAME("avr25");
  25. case bfd_mach_avr3:
  26. return AVR_CPU_TYPE_NAME("avr3");
  27. case bfd_mach_avr31:
  28. return AVR_CPU_TYPE_NAME("avr31");
  29. case bfd_mach_avr35:
  30. return AVR_CPU_TYPE_NAME("avr35");
  31. case bfd_mach_avr4:
  32. return AVR_CPU_TYPE_NAME("avr4");
  33. case bfd_mach_avr5:
  34. return AVR_CPU_TYPE_NAME("avr5");
  35. case bfd_mach_avr51:
  36. return AVR_CPU_TYPE_NAME("avr51");
  37. case bfd_mach_avr6:
  38. return AVR_CPU_TYPE_NAME("avr6");
  39. case bfd_mach_avrtiny:
  40. return AVR_CPU_TYPE_NAME("avrtiny");
  41. case bfd_mach_avrxmega2:
  42. return AVR_CPU_TYPE_NAME("xmega2");
  43. case bfd_mach_avrxmega3:
  44. return AVR_CPU_TYPE_NAME("xmega3");
  45. case bfd_mach_avrxmega4:
  46. return AVR_CPU_TYPE_NAME("xmega4");
  47. case bfd_mach_avrxmega5:
  48. return AVR_CPU_TYPE_NAME("xmega5");
  49. case bfd_mach_avrxmega6:
  50. return AVR_CPU_TYPE_NAME("xmega6");
  51. case bfd_mach_avrxmega7:
  52. return AVR_CPU_TYPE_NAME("xmega7");
  53. default:
  54. return NULL;
  55. }
  56. }
  57. bool avr_load_firmware(AVRCPU *cpu, MachineState *ms,
  58. MemoryRegion *program_mr, const char *firmware)
  59. {
  60. g_autofree char *filename = NULL;
  61. int bytes_loaded;
  62. uint64_t entry;
  63. uint32_t e_flags;
  64. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware);
  65. if (filename == NULL) {
  66. error_report("Unable to find %s", firmware);
  67. return false;
  68. }
  69. bytes_loaded = load_elf_as(filename, NULL, NULL, NULL,
  70. &entry, NULL, NULL,
  71. &e_flags, ELFDATA2LSB, EM_AVR, 0, 0, NULL);
  72. if (bytes_loaded >= 0) {
  73. /* If ELF file is provided, determine CPU type reading ELF e_flags. */
  74. const char *elf_cpu = avr_elf_e_flags_to_cpu_type(e_flags);
  75. const char *mcu_cpu_type = object_get_typename(OBJECT(cpu));
  76. int cpu_len = strlen(mcu_cpu_type) - strlen(AVR_CPU_TYPE_SUFFIX);
  77. if (entry) {
  78. error_report("BIOS entry_point must be 0x0000 "
  79. "(ELF image '%s' has entry_point 0x%04" PRIx64 ")",
  80. firmware, entry);
  81. return false;
  82. }
  83. if (!elf_cpu) {
  84. warn_report("Could not determine CPU type for ELF image '%s', "
  85. "assuming '%.*s' CPU",
  86. firmware, cpu_len, mcu_cpu_type);
  87. return true;
  88. }
  89. if (strcmp(elf_cpu, mcu_cpu_type)) {
  90. error_report("Current machine: %s with '%.*s' CPU",
  91. MACHINE_GET_CLASS(ms)->desc, cpu_len, mcu_cpu_type);
  92. error_report("ELF image '%s' is for '%.*s' CPU",
  93. firmware,
  94. (int)(strlen(elf_cpu) - strlen(AVR_CPU_TYPE_SUFFIX)),
  95. elf_cpu);
  96. return false;
  97. }
  98. } else {
  99. bytes_loaded = load_image_mr(filename, program_mr);
  100. }
  101. if (bytes_loaded < 0) {
  102. error_report("Unable to load firmware image %s as ELF or raw binary",
  103. firmware);
  104. return false;
  105. }
  106. return true;
  107. }