boot.c 3.8 KB

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