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/datadir.h"
13 #include "hw/loader.h"
16 #include "qemu/error-report.h"
18 static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags
)
20 switch (flags
& EF_AVR_MACH
) {
22 return AVR_CPU_TYPE_NAME("avr1");
24 return AVR_CPU_TYPE_NAME("avr2");
26 return AVR_CPU_TYPE_NAME("avr25");
28 return AVR_CPU_TYPE_NAME("avr3");
30 return AVR_CPU_TYPE_NAME("avr31");
32 return AVR_CPU_TYPE_NAME("avr35");
34 return AVR_CPU_TYPE_NAME("avr4");
36 return AVR_CPU_TYPE_NAME("avr5");
38 return AVR_CPU_TYPE_NAME("avr51");
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");
60 bool avr_load_firmware(AVRCPU
*cpu
, MachineState
*ms
,
61 MemoryRegion
*program_mr
, const char *firmware
)
63 g_autofree
char *filename
= NULL
;
68 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, firmware
);
69 if (filename
== NULL
) {
70 error_report("Unable to find %s", firmware
);
74 bytes_loaded
= load_elf_ram_sym(filename
,
77 &e_flags
, 0, EM_AVR
, 0, 0,
79 if (bytes_loaded
>= 0) {
80 /* If ELF file is provided, determine CPU type reading ELF e_flags. */
81 const char *elf_cpu
= avr_elf_e_flags_to_cpu_type(e_flags
);
82 const char *mcu_cpu_type
= object_get_typename(OBJECT(cpu
));
83 int cpu_len
= strlen(mcu_cpu_type
) - strlen(AVR_CPU_TYPE_SUFFIX
);
86 error_report("BIOS entry_point must be 0x0000 "
87 "(ELF image '%s' has entry_point 0x%04" PRIx64
")",
92 warn_report("Could not determine CPU type for ELF image '%s', "
93 "assuming '%.*s' CPU",
94 firmware
, cpu_len
, mcu_cpu_type
);
97 if (strcmp(elf_cpu
, mcu_cpu_type
)) {
98 error_report("Current machine: %s with '%.*s' CPU",
99 MACHINE_GET_CLASS(ms
)->desc
, cpu_len
, mcu_cpu_type
);
100 error_report("ELF image '%s' is for '%.*s' CPU",
102 (int)(strlen(elf_cpu
) - strlen(AVR_CPU_TYPE_SUFFIX
)),
107 bytes_loaded
= load_image_mr(filename
, program_mr
);
109 if (bytes_loaded
< 0) {
110 error_report("Unable to load firmware image %s as ELF or raw binary",