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