]> git.proxmox.com Git - mirror_qemu.git/blame - hw/nvram/mac_nvram.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-ui-20161028-1' into staging
[mirror_qemu.git] / hw / nvram / mac_nvram.c
CommitLineData
3cbee15b
JM
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 */
0430891c 25#include "qemu/osdep.h"
83c9f4ca 26#include "hw/hw.h"
55d9950a 27#include "hw/nvram/chrp_nvram.h"
83c9f4ca 28#include "hw/ppc/mac.h"
f348b6d1 29#include "qemu/cutils.h"
2d9907a3 30#include <zlib.h>
3cbee15b 31
ea026b2f
BS
32/* debug NVR */
33//#define DEBUG_NVR
34
35#ifdef DEBUG_NVR
001faf32
BS
36#define NVR_DPRINTF(fmt, ...) \
37 do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0)
ea026b2f 38#else
001faf32 39#define NVR_DPRINTF(fmt, ...)
ea026b2f
BS
40#endif
41
bd89f43f
AJ
42#define DEF_SYSTEM_SIZE 0xc10
43
3cbee15b 44/* macio style NVRAM device */
a8170e5e 45static void macio_nvram_writeb(void *opaque, hwaddr addr,
23c5e4ca 46 uint64_t value, unsigned size)
3cbee15b
JM
47{
48 MacIONVRAMState *s = opaque;
74e91155 49
68af3f24 50 addr = (addr >> s->it_shift) & (s->size - 1);
3cbee15b 51 s->data[addr] = value;
2f448e41
AM
52 NVR_DPRINTF("writeb addr %04" HWADDR_PRIx " val %" PRIx64 "\n",
53 addr, value);
3cbee15b
JM
54}
55
a8170e5e 56static uint64_t macio_nvram_readb(void *opaque, hwaddr addr,
23c5e4ca 57 unsigned size)
3cbee15b
JM
58{
59 MacIONVRAMState *s = opaque;
60 uint32_t value;
61
68af3f24 62 addr = (addr >> s->it_shift) & (s->size - 1);
3cbee15b 63 value = s->data[addr];
2f448e41
AM
64 NVR_DPRINTF("readb addr %04" HWADDR_PRIx " val %" PRIx32 "\n",
65 addr, value);
3cbee15b
JM
66
67 return value;
68}
69
23c5e4ca
AK
70static const MemoryRegionOps macio_nvram_ops = {
71 .read = macio_nvram_readb,
72 .write = macio_nvram_writeb,
b19eae18
AG
73 .valid.min_access_size = 1,
74 .valid.max_access_size = 4,
75 .impl.min_access_size = 1,
76 .impl.max_access_size = 1,
d8c6d07f 77 .endianness = DEVICE_BIG_ENDIAN,
3cbee15b
JM
78};
79
8e470f8a
JQ
80static const VMStateDescription vmstate_macio_nvram = {
81 .name = "macio_nvram",
82 .version_id = 1,
83 .minimum_version_id = 1,
35d08458 84 .fields = (VMStateField[]) {
8e470f8a
JQ
85 VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size),
86 VMSTATE_END_OF_LIST()
87 }
88};
9b64997f 89
9b64997f 90
95ed3b7c 91static void macio_nvram_reset(DeviceState *dev)
6e6b7363
BS
92{
93}
94
95ed3b7c 95static void macio_nvram_realizefn(DeviceState *dev, Error **errp)
3cbee15b 96{
95ed3b7c
AF
97 SysBusDevice *d = SYS_BUS_DEVICE(dev);
98 MacIONVRAMState *s = MACIO_NVRAM(dev);
74e91155 99
95ed3b7c 100 s->data = g_malloc0(s->size);
3f7cbbbd 101
eedfac6f
PB
102 memory_region_init_io(&s->mem, OBJECT(s), &macio_nvram_ops, s,
103 "macio-nvram", s->size << s->it_shift);
95ed3b7c
AF
104 sysbus_init_mmio(d, &s->mem);
105}
106
107static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp)
108{
109 MacIONVRAMState *s = MACIO_NVRAM(dev);
110
111 g_free(s->data);
112}
3cbee15b 113
95ed3b7c
AF
114static Property macio_nvram_properties[] = {
115 DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0),
116 DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0),
117 DEFINE_PROP_END_OF_LIST()
118};
119
120static void macio_nvram_class_init(ObjectClass *oc, void *data)
121{
122 DeviceClass *dc = DEVICE_CLASS(oc);
123
124 dc->realize = macio_nvram_realizefn;
125 dc->unrealize = macio_nvram_unrealizefn;
126 dc->reset = macio_nvram_reset;
127 dc->vmsd = &vmstate_macio_nvram;
128 dc->props = macio_nvram_properties;
175fe9e7 129 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
3cbee15b
JM
130}
131
95ed3b7c
AF
132static const TypeInfo macio_nvram_type_info = {
133 .name = TYPE_MACIO_NVRAM,
134 .parent = TYPE_SYS_BUS_DEVICE,
135 .instance_size = sizeof(MacIONVRAMState),
136 .class_init = macio_nvram_class_init,
137};
138
139static void macio_nvram_register_types(void)
74e91155 140{
95ed3b7c 141 type_register_static(&macio_nvram_type_info);
74e91155
JM
142}
143
95efd11c 144/* Set up a system OpenBIOS NVRAM partition */
2d9907a3
AG
145static void pmac_format_nvram_partition_of(MacIONVRAMState *nvr, int off,
146 int len)
3cbee15b 147{
55d9950a
TH
148 int sysp_end;
149
150 /* OpenBIOS nvram variables partition */
151 sysp_end = chrp_nvram_create_system_partition(&nvr->data[off],
152 DEF_SYSTEM_SIZE) + off;
153
154 /* Free space partition */
155 chrp_nvram_create_free_partition(&nvr->data[sysp_end], len - sysp_end);
3cbee15b 156}
95ed3b7c 157
2d9907a3
AG
158#define OSX_NVRAM_SIGNATURE (0x5A)
159
160/* Set up a Mac OS X NVRAM partition */
161static void pmac_format_nvram_partition_osx(MacIONVRAMState *nvr, int off,
162 int len)
163{
164 uint32_t start = off;
ad723fe5 165 ChrpNvramPartHdr *part_header;
2d9907a3
AG
166 unsigned char *data = &nvr->data[start];
167
168 /* empty partition */
ad723fe5 169 part_header = (ChrpNvramPartHdr *)data;
2d9907a3
AG
170 part_header->signature = OSX_NVRAM_SIGNATURE;
171 pstrcpy(part_header->name, sizeof(part_header->name), "wwwwwwwwwwww");
172
ad723fe5 173 chrp_nvram_finish_partition(part_header, len);
2d9907a3
AG
174
175 /* Generation */
176 stl_be_p(&data[20], 2);
177
178 /* Adler32 checksum */
179 stl_be_p(&data[16], adler32(0, &data[20], len - 20));
180}
181
182/* Set up NVRAM with OF and OSX partitions */
183void pmac_format_nvram_partition(MacIONVRAMState *nvr, int len)
184{
185 /*
186 * Mac OS X expects side "B" of the flash at the second half of NVRAM,
187 * so we use half of the chip for OF and the other half for a free OSX
188 * partition.
189 */
190 pmac_format_nvram_partition_of(nvr, 0, len / 2);
191 pmac_format_nvram_partition_osx(nvr, len / 2, len / 2);
192}
95ed3b7c 193type_init(macio_nvram_register_types)