1 /* 2 * PowerMac NVRAM emulation 3 * 4 * Copyright (c) 2005-2007 Fabrice Bellard 5 * Copyright (c) 2007 Jocelyn Mayer 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 #include "hw/hw.h" 26 #include "hw/nvram/openbios_firmware_abi.h" 27 #include "sysemu/sysemu.h" 28 #include "hw/ppc/mac.h" 29 30 /* debug NVR */ 31 //#define DEBUG_NVR 32 33 #ifdef DEBUG_NVR 34 #define NVR_DPRINTF(fmt, ...) \ 35 do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0) 36 #else 37 #define NVR_DPRINTF(fmt, ...) 38 #endif 39 40 #define DEF_SYSTEM_SIZE 0xc10 41 42 /* Direct access to NVRAM */ 43 uint8_t macio_nvram_read(MacIONVRAMState *s, uint32_t addr) 44 { 45 uint32_t ret; 46 47 if (addr < s->size) { 48 ret = s->data[addr]; 49 } else { 50 ret = -1; 51 } 52 NVR_DPRINTF("read addr %04" PRIx32 " val %" PRIx8 "\n", addr, ret); 53 54 return ret; 55 } 56 57 void macio_nvram_write(MacIONVRAMState *s, uint32_t addr, uint8_t val) 58 { 59 NVR_DPRINTF("write addr %04" PRIx32 " val %" PRIx8 "\n", addr, val); 60 if (addr < s->size) { 61 s->data[addr] = val; 62 } 63 } 64 65 /* macio style NVRAM device */ 66 static void macio_nvram_writeb(void *opaque, hwaddr addr, 67 uint64_t value, unsigned size) 68 { 69 MacIONVRAMState *s = opaque; 70 71 addr = (addr >> s->it_shift) & (s->size - 1); 72 s->data[addr] = value; 73 NVR_DPRINTF("writeb addr %04" PHYS_PRIx " val %" PRIx64 "\n", addr, value); 74 } 75 76 static uint64_t macio_nvram_readb(void *opaque, hwaddr addr, 77 unsigned size) 78 { 79 MacIONVRAMState *s = opaque; 80 uint32_t value; 81 82 addr = (addr >> s->it_shift) & (s->size - 1); 83 value = s->data[addr]; 84 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value); 85 86 return value; 87 } 88 89 static const MemoryRegionOps macio_nvram_ops = { 90 .read = macio_nvram_readb, 91 .write = macio_nvram_writeb, 92 .endianness = DEVICE_BIG_ENDIAN, 93 }; 94 95 static const VMStateDescription vmstate_macio_nvram = { 96 .name = "macio_nvram", 97 .version_id = 1, 98 .minimum_version_id = 1, 99 .fields = (VMStateField[]) { 100 VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size), 101 VMSTATE_END_OF_LIST() 102 } 103 }; 104 105 106 static void macio_nvram_reset(DeviceState *dev) 107 { 108 } 109 110 static void macio_nvram_realizefn(DeviceState *dev, Error **errp) 111 { 112 SysBusDevice *d = SYS_BUS_DEVICE(dev); 113 MacIONVRAMState *s = MACIO_NVRAM(dev); 114 115 s->data = g_malloc0(s->size); 116 117 memory_region_init_io(&s->mem, OBJECT(s), &macio_nvram_ops, s, 118 "macio-nvram", s->size << s->it_shift); 119 sysbus_init_mmio(d, &s->mem); 120 } 121 122 static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp) 123 { 124 MacIONVRAMState *s = MACIO_NVRAM(dev); 125 126 g_free(s->data); 127 } 128 129 static Property macio_nvram_properties[] = { 130 DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0), 131 DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0), 132 DEFINE_PROP_END_OF_LIST() 133 }; 134 135 static void macio_nvram_class_init(ObjectClass *oc, void *data) 136 { 137 DeviceClass *dc = DEVICE_CLASS(oc); 138 139 dc->realize = macio_nvram_realizefn; 140 dc->unrealize = macio_nvram_unrealizefn; 141 dc->reset = macio_nvram_reset; 142 dc->vmsd = &vmstate_macio_nvram; 143 dc->props = macio_nvram_properties; 144 } 145 146 static const TypeInfo macio_nvram_type_info = { 147 .name = TYPE_MACIO_NVRAM, 148 .parent = TYPE_SYS_BUS_DEVICE, 149 .instance_size = sizeof(MacIONVRAMState), 150 .class_init = macio_nvram_class_init, 151 }; 152 153 static void macio_nvram_register_types(void) 154 { 155 type_register_static(&macio_nvram_type_info); 156 } 157 158 /* Set up a system OpenBIOS NVRAM partition */ 159 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len) 160 { 161 unsigned int i; 162 uint32_t start = 0, end; 163 struct OpenBIOS_nvpart_v1 *part_header; 164 165 // OpenBIOS nvram variables 166 // Variable partition 167 part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data; 168 part_header->signature = OPENBIOS_PART_SYSTEM; 169 pstrcpy(part_header->name, sizeof(part_header->name), "system"); 170 171 end = start + sizeof(struct OpenBIOS_nvpart_v1); 172 for (i = 0; i < nb_prom_envs; i++) 173 end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]); 174 175 // End marker 176 nvr->data[end++] = '\0'; 177 178 end = start + ((end - start + 15) & ~15); 179 /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for 180 new variables. */ 181 if (end < DEF_SYSTEM_SIZE) 182 end = DEF_SYSTEM_SIZE; 183 OpenBIOS_finish_partition(part_header, end - start); 184 185 // free partition 186 start = end; 187 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start]; 188 part_header->signature = OPENBIOS_PART_FREE; 189 pstrcpy(part_header->name, sizeof(part_header->name), "free"); 190 191 end = len; 192 OpenBIOS_finish_partition(part_header, end - start); 193 } 194 195 type_init(macio_nvram_register_types) 196