4 * Copyright (c) 2019-2020 Philippe Mathieu-Daudé
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
11 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 #include "qemu/datadir.h"
14 #include "hw/loader.h"
17 #include "qemu/error-report.h"
19 static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags
)
21 switch (flags
& EF_AVR_MACH
) {
23 return AVR_CPU_TYPE_NAME("avr1");
25 return AVR_CPU_TYPE_NAME("avr2");
27 return AVR_CPU_TYPE_NAME("avr25");
29 return AVR_CPU_TYPE_NAME("avr3");
31 return AVR_CPU_TYPE_NAME("avr31");
33 return AVR_CPU_TYPE_NAME("avr35");
35 return AVR_CPU_TYPE_NAME("avr4");
37 return AVR_CPU_TYPE_NAME("avr5");
39 return AVR_CPU_TYPE_NAME("avr51");
41 return AVR_CPU_TYPE_NAME("avr6");
42 case bfd_mach_avrtiny
:
43 return AVR_CPU_TYPE_NAME("avrtiny");
44 case bfd_mach_avrxmega2
:
45 return AVR_CPU_TYPE_NAME("xmega2");
46 case bfd_mach_avrxmega3
:
47 return AVR_CPU_TYPE_NAME("xmega3");
48 case bfd_mach_avrxmega4
:
49 return AVR_CPU_TYPE_NAME("xmega4");
50 case bfd_mach_avrxmega5
:
51 return AVR_CPU_TYPE_NAME("xmega5");
52 case bfd_mach_avrxmega6
:
53 return AVR_CPU_TYPE_NAME("xmega6");
54 case bfd_mach_avrxmega7
:
55 return AVR_CPU_TYPE_NAME("xmega7");
61 bool avr_load_firmware(AVRCPU
*cpu
, MachineState
*ms
,
62 MemoryRegion
*program_mr
, const char *firmware
)
64 g_autofree
char *filename
= NULL
;
69 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, firmware
);
70 if (filename
== NULL
) {
71 error_report("Unable to find %s", firmware
);
75 bytes_loaded
= load_elf_ram_sym(filename
,
78 &e_flags
, 0, EM_AVR
, 0, 0,
80 if (bytes_loaded
>= 0) {
81 /* If ELF file is provided, determine CPU type reading ELF e_flags. */
82 const char *elf_cpu
= avr_elf_e_flags_to_cpu_type(e_flags
);
83 const char *mcu_cpu_type
= object_get_typename(OBJECT(cpu
));
84 int cpu_len
= strlen(mcu_cpu_type
) - strlen(AVR_CPU_TYPE_SUFFIX
);
87 error_report("BIOS entry_point must be 0x0000 "
88 "(ELF image '%s' has entry_point 0x%04" PRIx64
")",
93 warn_report("Could not determine CPU type for ELF image '%s', "
94 "assuming '%.*s' CPU",
95 firmware
, cpu_len
, mcu_cpu_type
);
98 if (strcmp(elf_cpu
, mcu_cpu_type
)) {
99 error_report("Current machine: %s with '%.*s' CPU",
100 MACHINE_GET_CLASS(ms
)->desc
, cpu_len
, mcu_cpu_type
);
101 error_report("ELF image '%s' is for '%.*s' CPU",
103 (int)(strlen(elf_cpu
) - strlen(AVR_CPU_TYPE_SUFFIX
)),
108 bytes_loaded
= load_image_mr(filename
, program_mr
);
110 if (bytes_loaded
< 0) {
111 error_report("Unable to load firmware image %s as ELF or raw binary",