mac_nvram.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/nvram/chrp_nvram.h"
  27. #include "hw/ppc/mac.h"
  28. #include "hw/qdev-properties.h"
  29. #include "migration/vmstate.h"
  30. #include "qemu/cutils.h"
  31. #include "qemu/module.h"
  32. #include <zlib.h>
  33. /* debug NVR */
  34. //#define DEBUG_NVR
  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. #define DEF_SYSTEM_SIZE 0xc10
  42. /* macio style NVRAM device */
  43. static void macio_nvram_writeb(void *opaque, hwaddr addr,
  44. uint64_t value, unsigned size)
  45. {
  46. MacIONVRAMState *s = opaque;
  47. addr = (addr >> s->it_shift) & (s->size - 1);
  48. s->data[addr] = value;
  49. NVR_DPRINTF("writeb addr %04" HWADDR_PRIx " val %" PRIx64 "\n",
  50. addr, value);
  51. }
  52. static uint64_t macio_nvram_readb(void *opaque, hwaddr addr,
  53. unsigned size)
  54. {
  55. MacIONVRAMState *s = opaque;
  56. uint32_t value;
  57. addr = (addr >> s->it_shift) & (s->size - 1);
  58. value = s->data[addr];
  59. NVR_DPRINTF("readb addr %04" HWADDR_PRIx " val %" PRIx32 "\n",
  60. addr, value);
  61. return value;
  62. }
  63. static const MemoryRegionOps macio_nvram_ops = {
  64. .read = macio_nvram_readb,
  65. .write = macio_nvram_writeb,
  66. .valid.min_access_size = 1,
  67. .valid.max_access_size = 4,
  68. .impl.min_access_size = 1,
  69. .impl.max_access_size = 1,
  70. .endianness = DEVICE_BIG_ENDIAN,
  71. };
  72. static const VMStateDescription vmstate_macio_nvram = {
  73. .name = "macio_nvram",
  74. .version_id = 1,
  75. .minimum_version_id = 1,
  76. .fields = (VMStateField[]) {
  77. VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, size),
  78. VMSTATE_END_OF_LIST()
  79. }
  80. };
  81. static void macio_nvram_reset(DeviceState *dev)
  82. {
  83. }
  84. static void macio_nvram_realizefn(DeviceState *dev, Error **errp)
  85. {
  86. SysBusDevice *d = SYS_BUS_DEVICE(dev);
  87. MacIONVRAMState *s = MACIO_NVRAM(dev);
  88. s->data = g_malloc0(s->size);
  89. memory_region_init_io(&s->mem, OBJECT(s), &macio_nvram_ops, s,
  90. "macio-nvram", s->size << s->it_shift);
  91. sysbus_init_mmio(d, &s->mem);
  92. }
  93. static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp)
  94. {
  95. MacIONVRAMState *s = MACIO_NVRAM(dev);
  96. g_free(s->data);
  97. }
  98. static Property macio_nvram_properties[] = {
  99. DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0),
  100. DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0),
  101. DEFINE_PROP_END_OF_LIST()
  102. };
  103. static void macio_nvram_class_init(ObjectClass *oc, void *data)
  104. {
  105. DeviceClass *dc = DEVICE_CLASS(oc);
  106. dc->realize = macio_nvram_realizefn;
  107. dc->unrealize = macio_nvram_unrealizefn;
  108. dc->reset = macio_nvram_reset;
  109. dc->vmsd = &vmstate_macio_nvram;
  110. dc->props = macio_nvram_properties;
  111. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  112. }
  113. static const TypeInfo macio_nvram_type_info = {
  114. .name = TYPE_MACIO_NVRAM,
  115. .parent = TYPE_SYS_BUS_DEVICE,
  116. .instance_size = sizeof(MacIONVRAMState),
  117. .class_init = macio_nvram_class_init,
  118. };
  119. static void macio_nvram_register_types(void)
  120. {
  121. type_register_static(&macio_nvram_type_info);
  122. }
  123. /* Set up a system OpenBIOS NVRAM partition */
  124. static void pmac_format_nvram_partition_of(MacIONVRAMState *nvr, int off,
  125. int len)
  126. {
  127. int sysp_end;
  128. /* OpenBIOS nvram variables partition */
  129. sysp_end = chrp_nvram_create_system_partition(&nvr->data[off],
  130. DEF_SYSTEM_SIZE) + off;
  131. /* Free space partition */
  132. chrp_nvram_create_free_partition(&nvr->data[sysp_end], len - sysp_end);
  133. }
  134. #define OSX_NVRAM_SIGNATURE (0x5A)
  135. /* Set up a Mac OS X NVRAM partition */
  136. static void pmac_format_nvram_partition_osx(MacIONVRAMState *nvr, int off,
  137. int len)
  138. {
  139. uint32_t start = off;
  140. ChrpNvramPartHdr *part_header;
  141. unsigned char *data = &nvr->data[start];
  142. /* empty partition */
  143. part_header = (ChrpNvramPartHdr *)data;
  144. part_header->signature = OSX_NVRAM_SIGNATURE;
  145. pstrcpy(part_header->name, sizeof(part_header->name), "wwwwwwwwwwww");
  146. chrp_nvram_finish_partition(part_header, len);
  147. /* Generation */
  148. stl_be_p(&data[20], 2);
  149. /* Adler32 checksum */
  150. stl_be_p(&data[16], adler32(0, &data[20], len - 20));
  151. }
  152. /* Set up NVRAM with OF and OSX partitions */
  153. void pmac_format_nvram_partition(MacIONVRAMState *nvr, int len)
  154. {
  155. /*
  156. * Mac OS X expects side "B" of the flash at the second half of NVRAM,
  157. * so we use half of the chip for OF and the other half for a free OSX
  158. * partition.
  159. */
  160. pmac_format_nvram_partition_of(nvr, 0, len / 2);
  161. pmac_format_nvram_partition_osx(nvr, len / 2, len / 2);
  162. }
  163. type_init(macio_nvram_register_types)