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 "qemu/osdep.h" 26 #include "hw/hw.h" 27 #include "hw/nvram/openbios_firmware_abi.h" 28 #include "sysemu/sysemu.h" 29 #include "hw/ppc/mac.h" 30 #include <zlib.h> 31 32 /* debug NVR */ 33 //#define DEBUG_NVR 34 35 #ifdef DEBUG_NVR 36 #define NVR_DPRINTF(fmt, ...) \ 37 do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0) 38 #else 39 #define NVR_DPRINTF(fmt, ...) 40 #endif 41 42 #define DEF_SYSTEM_SIZE 0xc10 43 44 /* macio style NVRAM device */ 45 static void macio_nvram_writeb(void *opaque, hwaddr addr, 46 uint64_t value, unsigned size) 47 { 48 MacIONVRAMState *s = opaque; 49 50 addr = (addr >> s->it_shift) & (s->size - 1); 51 s->data[addr] = value; 52 NVR_DPRINTF("writeb addr %04" PHYS_PRIx " val %" PRIx64 "\n", addr, value); 53 } 54 55 static uint64_t macio_nvram_readb(void *opaque, hwaddr addr, 56 unsigned size) 57 { 58 MacIONVRAMState *s = opaque; 59 uint32_t value; 60 61 addr = (addr >> s->it_shift) & (s->size - 1); 62 value = s->data[addr]; 63 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value); 64 65 return value; 66 } 67 68 static const MemoryRegionOps macio_nvram_ops = { 69 .read = macio_nvram_readb, 70 .write = macio_nvram_writeb, 71 .valid.min_access_size = 1, 72 .valid.max_access_size = 4, 73 .impl.min_access_size = 1, 74 .impl.max_access_size = 1, 75 .endianness = DEVICE_BIG_ENDIAN, 76 }; 77 78 static const VMStateDescription vmstate_macio_nvram = { 79 .name = "macio_nvram", 80 .version_id = 1, 81 .minimum_version_id = 1, 82 .fields = (VMStateField[]) { 83 VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size), 84 VMSTATE_END_OF_LIST() 85 } 86 }; 87 88 89 static void macio_nvram_reset(DeviceState *dev) 90 { 91 } 92 93 static void macio_nvram_realizefn(DeviceState *dev, Error **errp) 94 { 95 SysBusDevice *d = SYS_BUS_DEVICE(dev); 96 MacIONVRAMState *s = MACIO_NVRAM(dev); 97 98 s->data = g_malloc0(s->size); 99 100 memory_region_init_io(&s->mem, OBJECT(s), &macio_nvram_ops, s, 101 "macio-nvram", s->size << s->it_shift); 102 sysbus_init_mmio(d, &s->mem); 103 } 104 105 static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp) 106 { 107 MacIONVRAMState *s = MACIO_NVRAM(dev); 108 109 g_free(s->data); 110 } 111 112 static Property macio_nvram_properties[] = { 113 DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0), 114 DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0), 115 DEFINE_PROP_END_OF_LIST() 116 }; 117 118 static void macio_nvram_class_init(ObjectClass *oc, void *data) 119 { 120 DeviceClass *dc = DEVICE_CLASS(oc); 121 122 dc->realize = macio_nvram_realizefn; 123 dc->unrealize = macio_nvram_unrealizefn; 124 dc->reset = macio_nvram_reset; 125 dc->vmsd = &vmstate_macio_nvram; 126 dc->props = macio_nvram_properties; 127 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 128 } 129 130 static const TypeInfo macio_nvram_type_info = { 131 .name = TYPE_MACIO_NVRAM, 132 .parent = TYPE_SYS_BUS_DEVICE, 133 .instance_size = sizeof(MacIONVRAMState), 134 .class_init = macio_nvram_class_init, 135 }; 136 137 static void macio_nvram_register_types(void) 138 { 139 type_register_static(&macio_nvram_type_info); 140 } 141 142 /* Set up a system OpenBIOS NVRAM partition */ 143 static void pmac_format_nvram_partition_of(MacIONVRAMState *nvr, int off, 144 int len) 145 { 146 unsigned int i; 147 uint32_t start = off, end; 148 struct OpenBIOS_nvpart_v1 *part_header; 149 150 // OpenBIOS nvram variables 151 // Variable partition 152 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start]; 153 part_header->signature = OPENBIOS_PART_SYSTEM; 154 pstrcpy(part_header->name, sizeof(part_header->name), "system"); 155 156 end = start + sizeof(struct OpenBIOS_nvpart_v1); 157 for (i = 0; i < nb_prom_envs; i++) 158 end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]); 159 160 // End marker 161 nvr->data[end++] = '\0'; 162 163 end = start + ((end - start + 15) & ~15); 164 /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for 165 new variables. */ 166 if (end < DEF_SYSTEM_SIZE) 167 end = DEF_SYSTEM_SIZE; 168 OpenBIOS_finish_partition(part_header, end - start); 169 170 // free partition 171 start = end; 172 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start]; 173 part_header->signature = OPENBIOS_PART_FREE; 174 pstrcpy(part_header->name, sizeof(part_header->name), "free"); 175 176 end = len; 177 OpenBIOS_finish_partition(part_header, end - start); 178 } 179 180 #define OSX_NVRAM_SIGNATURE (0x5A) 181 182 /* Set up a Mac OS X NVRAM partition */ 183 static void pmac_format_nvram_partition_osx(MacIONVRAMState *nvr, int off, 184 int len) 185 { 186 uint32_t start = off; 187 struct OpenBIOS_nvpart_v1 *part_header; 188 unsigned char *data = &nvr->data[start]; 189 190 /* empty partition */ 191 part_header = (struct OpenBIOS_nvpart_v1 *)data; 192 part_header->signature = OSX_NVRAM_SIGNATURE; 193 pstrcpy(part_header->name, sizeof(part_header->name), "wwwwwwwwwwww"); 194 195 OpenBIOS_finish_partition(part_header, len); 196 197 /* Generation */ 198 stl_be_p(&data[20], 2); 199 200 /* Adler32 checksum */ 201 stl_be_p(&data[16], adler32(0, &data[20], len - 20)); 202 } 203 204 /* Set up NVRAM with OF and OSX partitions */ 205 void pmac_format_nvram_partition(MacIONVRAMState *nvr, int len) 206 { 207 /* 208 * Mac OS X expects side "B" of the flash at the second half of NVRAM, 209 * so we use half of the chip for OF and the other half for a free OSX 210 * partition. 211 */ 212 pmac_format_nvram_partition_of(nvr, 0, len / 2); 213 pmac_format_nvram_partition_osx(nvr, len / 2, len / 2); 214 } 215 type_init(macio_nvram_register_types) 216