scripts/kernel-doc: parse __ETHTOOL_DECLARE_LINK_MODE_MASK
[qemu/ar7.git] / hw / avr / boot.c
blobcbede775cee2d359a3710c3a3755d28214fe071a
1 /*
2 * AVR loader helpers
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
9 */
11 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 #include "qemu/datadir.h"
14 #include "hw/loader.h"
15 #include "elf.h"
16 #include "boot.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) {
22 case bfd_mach_avr1:
23 return AVR_CPU_TYPE_NAME("avr1");
24 case bfd_mach_avr2:
25 return AVR_CPU_TYPE_NAME("avr2");
26 case bfd_mach_avr25:
27 return AVR_CPU_TYPE_NAME("avr25");
28 case bfd_mach_avr3:
29 return AVR_CPU_TYPE_NAME("avr3");
30 case bfd_mach_avr31:
31 return AVR_CPU_TYPE_NAME("avr31");
32 case bfd_mach_avr35:
33 return AVR_CPU_TYPE_NAME("avr35");
34 case bfd_mach_avr4:
35 return AVR_CPU_TYPE_NAME("avr4");
36 case bfd_mach_avr5:
37 return AVR_CPU_TYPE_NAME("avr5");
38 case bfd_mach_avr51:
39 return AVR_CPU_TYPE_NAME("avr51");
40 case bfd_mach_avr6:
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");
56 default:
57 return NULL;
61 bool avr_load_firmware(AVRCPU *cpu, MachineState *ms,
62 MemoryRegion *program_mr, const char *firmware)
64 g_autofree char *filename = NULL;
65 int bytes_loaded;
66 uint64_t entry;
67 uint32_t e_flags;
69 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware);
70 if (filename == NULL) {
71 error_report("Unable to find %s", firmware);
72 return false;
75 bytes_loaded = load_elf_ram_sym(filename,
76 NULL, NULL, NULL,
77 &entry, NULL, NULL,
78 &e_flags, 0, EM_AVR, 0, 0,
79 NULL, true, NULL);
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);
86 if (entry) {
87 error_report("BIOS entry_point must be 0x0000 "
88 "(ELF image '%s' has entry_point 0x%04" PRIx64 ")",
89 firmware, entry);
90 return false;
92 if (!elf_cpu) {
93 warn_report("Could not determine CPU type for ELF image '%s', "
94 "assuming '%.*s' CPU",
95 firmware, cpu_len, mcu_cpu_type);
96 return true;
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",
102 firmware,
103 (int)(strlen(elf_cpu) - strlen(AVR_CPU_TYPE_SUFFIX)),
104 elf_cpu);
105 return false;
107 } else {
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",
112 firmware);
113 return false;
115 return true;