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