hw/arm/orangepi: check for potential NULL pointer when calling blk_is_available
[qemu.git] / hw / arm / orangepi.c
1 /*
2 * Orange Pi emulation
3 *
4 * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
22 #include "exec/address-spaces.h"
23 #include "qapi/error.h"
24 #include "cpu.h"
25 #include "hw/sysbus.h"
26 #include "hw/boards.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/arm/allwinner-h3.h"
29 #include "sysemu/sysemu.h"
30
31 static struct arm_boot_info orangepi_binfo = {
32 .nb_cpus = AW_H3_NUM_CPUS,
33 };
34
35 static void orangepi_init(MachineState *machine)
36 {
37 AwH3State *h3;
38 DriveInfo *di;
39 BlockBackend *blk;
40 BusState *bus;
41 DeviceState *carddev;
42
43 /* BIOS is not supported by this board */
44 if (bios_name) {
45 error_report("BIOS not supported for this machine");
46 exit(1);
47 }
48
49 /* This board has fixed size RAM */
50 if (machine->ram_size != 1 * GiB) {
51 error_report("This machine can only be used with 1GiB of RAM");
52 exit(1);
53 }
54
55 /* Only allow Cortex-A7 for this board */
56 if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a7")) != 0) {
57 error_report("This board can only be used with cortex-a7 CPU");
58 exit(1);
59 }
60
61 h3 = AW_H3(object_new(TYPE_AW_H3));
62 object_property_add_child(OBJECT(machine), "soc", OBJECT(h3),
63 &error_abort);
64 object_unref(OBJECT(h3));
65
66 /* Setup timer properties */
67 object_property_set_int(OBJECT(h3), 32768, "clk0-freq",
68 &error_abort);
69 object_property_set_int(OBJECT(h3), 24 * 1000 * 1000, "clk1-freq",
70 &error_abort);
71
72 /* Setup SID properties. Currently using a default fixed SID identifier. */
73 if (qemu_uuid_is_null(&h3->sid.identifier)) {
74 qdev_prop_set_string(DEVICE(h3), "identifier",
75 "02c00081-1111-2222-3333-000044556677");
76 } else if (ldl_be_p(&h3->sid.identifier.data[0]) != 0x02c00081) {
77 warn_report("Security Identifier value does not include H3 prefix");
78 }
79
80 /* Setup EMAC properties */
81 object_property_set_int(OBJECT(&h3->emac), 1, "phy-addr", &error_abort);
82
83 /* DRAMC */
84 object_property_set_uint(OBJECT(h3), h3->memmap[AW_H3_SDRAM],
85 "ram-addr", &error_abort);
86 object_property_set_int(OBJECT(h3), machine->ram_size / MiB, "ram-size",
87 &error_abort);
88
89 /* Mark H3 object realized */
90 object_property_set_bool(OBJECT(h3), true, "realized", &error_abort);
91
92 /* Retrieve SD bus */
93 di = drive_get_next(IF_SD);
94 blk = di ? blk_by_legacy_dinfo(di) : NULL;
95 bus = qdev_get_child_bus(DEVICE(h3), "sd-bus");
96
97 /* Plug in SD card */
98 carddev = qdev_create(bus, TYPE_SD_CARD);
99 qdev_prop_set_drive(carddev, "drive", blk, &error_fatal);
100 object_property_set_bool(OBJECT(carddev), true, "realized", &error_fatal);
101
102 /* SDRAM */
103 memory_region_add_subregion(get_system_memory(), h3->memmap[AW_H3_SDRAM],
104 machine->ram);
105
106 /* Load target kernel or start using BootROM */
107 if (!machine->kernel_filename && blk && blk_is_available(blk)) {
108 /* Use Boot ROM to copy data from SD card to SRAM */
109 allwinner_h3_bootrom_setup(h3, blk);
110 }
111 orangepi_binfo.loader_start = h3->memmap[AW_H3_SDRAM];
112 orangepi_binfo.ram_size = machine->ram_size;
113 arm_load_kernel(ARM_CPU(first_cpu), machine, &orangepi_binfo);
114 }
115
116 static void orangepi_machine_init(MachineClass *mc)
117 {
118 mc->desc = "Orange Pi PC";
119 mc->init = orangepi_init;
120 mc->block_default_type = IF_SD;
121 mc->units_per_default_bus = 1;
122 mc->min_cpus = AW_H3_NUM_CPUS;
123 mc->max_cpus = AW_H3_NUM_CPUS;
124 mc->default_cpus = AW_H3_NUM_CPUS;
125 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
126 mc->default_ram_size = 1 * GiB;
127 mc->default_ram_id = "orangepi.ram";
128 }
129
130 DEFINE_MACHINE("orangepi-pc", orangepi_machine_init)