2
0

mac_nvram.c 5.7 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 "hw.h"
  26. #include "firmware_abi.h"
  27. #include "sysemu/sysemu.h"
  28. #include "ppc/mac.h"
  29. /* debug NVR */
  30. //#define DEBUG_NVR
  31. #ifdef DEBUG_NVR
  32. #define NVR_DPRINTF(fmt, ...) \
  33. do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0)
  34. #else
  35. #define NVR_DPRINTF(fmt, ...)
  36. #endif
  37. #define DEF_SYSTEM_SIZE 0xc10
  38. /* Direct access to NVRAM */
  39. uint8_t macio_nvram_read(MacIONVRAMState *s, uint32_t addr)
  40. {
  41. uint32_t ret;
  42. if (addr < s->size) {
  43. ret = s->data[addr];
  44. } else {
  45. ret = -1;
  46. }
  47. NVR_DPRINTF("read addr %04" PRIx32 " val %" PRIx8 "\n", addr, ret);
  48. return ret;
  49. }
  50. void macio_nvram_write(MacIONVRAMState *s, uint32_t addr, uint8_t val)
  51. {
  52. NVR_DPRINTF("write addr %04" PRIx32 " val %" PRIx8 "\n", addr, val);
  53. if (addr < s->size) {
  54. s->data[addr] = val;
  55. }
  56. }
  57. /* macio style NVRAM device */
  58. static void macio_nvram_writeb(void *opaque, hwaddr addr,
  59. uint64_t value, unsigned size)
  60. {
  61. MacIONVRAMState *s = opaque;
  62. addr = (addr >> s->it_shift) & (s->size - 1);
  63. s->data[addr] = value;
  64. NVR_DPRINTF("writeb addr %04" PHYS_PRIx " val %" PRIx64 "\n", addr, value);
  65. }
  66. static uint64_t macio_nvram_readb(void *opaque, hwaddr addr,
  67. unsigned size)
  68. {
  69. MacIONVRAMState *s = opaque;
  70. uint32_t value;
  71. addr = (addr >> s->it_shift) & (s->size - 1);
  72. value = s->data[addr];
  73. NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
  74. return value;
  75. }
  76. static const MemoryRegionOps macio_nvram_ops = {
  77. .read = macio_nvram_readb,
  78. .write = macio_nvram_writeb,
  79. .endianness = DEVICE_BIG_ENDIAN,
  80. };
  81. static const VMStateDescription vmstate_macio_nvram = {
  82. .name = "macio_nvram",
  83. .version_id = 1,
  84. .minimum_version_id = 1,
  85. .minimum_version_id_old = 1,
  86. .fields = (VMStateField[]) {
  87. VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size),
  88. VMSTATE_END_OF_LIST()
  89. }
  90. };
  91. static void macio_nvram_reset(DeviceState *dev)
  92. {
  93. }
  94. static void macio_nvram_realizefn(DeviceState *dev, Error **errp)
  95. {
  96. SysBusDevice *d = SYS_BUS_DEVICE(dev);
  97. MacIONVRAMState *s = MACIO_NVRAM(dev);
  98. s->data = g_malloc0(s->size);
  99. memory_region_init_io(&s->mem, &macio_nvram_ops, s, "macio-nvram",
  100. s->size << s->it_shift);
  101. sysbus_init_mmio(d, &s->mem);
  102. }
  103. static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp)
  104. {
  105. MacIONVRAMState *s = MACIO_NVRAM(dev);
  106. g_free(s->data);
  107. }
  108. static Property macio_nvram_properties[] = {
  109. DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0),
  110. DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0),
  111. DEFINE_PROP_END_OF_LIST()
  112. };
  113. static void macio_nvram_class_init(ObjectClass *oc, void *data)
  114. {
  115. DeviceClass *dc = DEVICE_CLASS(oc);
  116. dc->realize = macio_nvram_realizefn;
  117. dc->unrealize = macio_nvram_unrealizefn;
  118. dc->reset = macio_nvram_reset;
  119. dc->vmsd = &vmstate_macio_nvram;
  120. dc->props = macio_nvram_properties;
  121. }
  122. static const TypeInfo macio_nvram_type_info = {
  123. .name = TYPE_MACIO_NVRAM,
  124. .parent = TYPE_SYS_BUS_DEVICE,
  125. .instance_size = sizeof(MacIONVRAMState),
  126. .class_init = macio_nvram_class_init,
  127. };
  128. static void macio_nvram_register_types(void)
  129. {
  130. type_register_static(&macio_nvram_type_info);
  131. }
  132. /* Set up a system OpenBIOS NVRAM partition */
  133. void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
  134. {
  135. unsigned int i;
  136. uint32_t start = 0, end;
  137. struct OpenBIOS_nvpart_v1 *part_header;
  138. // OpenBIOS nvram variables
  139. // Variable partition
  140. part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
  141. part_header->signature = OPENBIOS_PART_SYSTEM;
  142. pstrcpy(part_header->name, sizeof(part_header->name), "system");
  143. end = start + sizeof(struct OpenBIOS_nvpart_v1);
  144. for (i = 0; i < nb_prom_envs; i++)
  145. end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
  146. // End marker
  147. nvr->data[end++] = '\0';
  148. end = start + ((end - start + 15) & ~15);
  149. /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
  150. new variables. */
  151. if (end < DEF_SYSTEM_SIZE)
  152. end = DEF_SYSTEM_SIZE;
  153. OpenBIOS_finish_partition(part_header, end - start);
  154. // free partition
  155. start = end;
  156. part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
  157. part_header->signature = OPENBIOS_PART_FREE;
  158. pstrcpy(part_header->name, sizeof(part_header->name), "free");
  159. end = len;
  160. OpenBIOS_finish_partition(part_header, end - start);
  161. }
  162. type_init(macio_nvram_register_types)