xref: /openbmc/qemu/hw/avr/boot.c (revision 9c4888c9)
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 
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 
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_ram_sym(filename,
75                                     NULL, NULL, NULL,
76                                     &entry, NULL, NULL,
77                                     &e_flags, 0, EM_AVR, 0, 0,
78                                     NULL, true, NULL);
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);
84 
85         if (entry) {
86             error_report("BIOS entry_point must be 0x0000 "
87                          "(ELF image '%s' has entry_point 0x%04" PRIx64 ")",
88                          firmware, entry);
89             return false;
90         }
91         if (!elf_cpu) {
92             warn_report("Could not determine CPU type for ELF image '%s', "
93                         "assuming '%.*s' CPU",
94                          firmware, cpu_len, mcu_cpu_type);
95             return true;
96         }
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",
101                          firmware,
102                          (int)(strlen(elf_cpu) - strlen(AVR_CPU_TYPE_SUFFIX)),
103                          elf_cpu);
104             return false;
105         }
106     } else {
107         bytes_loaded = load_image_mr(filename, program_mr);
108     }
109     if (bytes_loaded < 0) {
110         error_report("Unable to load firmware image %s as ELF or raw binary",
111                      firmware);
112         return false;
113     }
114     return true;
115 }
116