mac_nvram.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.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. struct MacIONVRAMState {
  38. uint32_t size;
  39. MemoryRegion mem;
  40. unsigned int it_shift;
  41. uint8_t *data;
  42. };
  43. #define DEF_SYSTEM_SIZE 0xc10
  44. /* Direct access to NVRAM */
  45. uint32_t macio_nvram_read (void *opaque, uint32_t addr)
  46. {
  47. MacIONVRAMState *s = opaque;
  48. uint32_t ret;
  49. if (addr < s->size)
  50. ret = s->data[addr];
  51. else
  52. ret = -1;
  53. NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
  54. return ret;
  55. }
  56. void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
  57. {
  58. MacIONVRAMState *s = opaque;
  59. NVR_DPRINTF("write addr %04x val %x\n", addr, val);
  60. if (addr < s->size)
  61. s->data[addr] = val;
  62. }
  63. /* macio style NVRAM device */
  64. static void macio_nvram_writeb(void *opaque, target_phys_addr_t addr,
  65. uint64_t value, unsigned size)
  66. {
  67. MacIONVRAMState *s = opaque;
  68. addr = (addr >> s->it_shift) & (s->size - 1);
  69. s->data[addr] = value;
  70. NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value);
  71. }
  72. static uint64_t macio_nvram_readb(void *opaque, target_phys_addr_t addr,
  73. unsigned size)
  74. {
  75. MacIONVRAMState *s = opaque;
  76. uint32_t value;
  77. addr = (addr >> s->it_shift) & (s->size - 1);
  78. value = s->data[addr];
  79. NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
  80. return value;
  81. }
  82. static const MemoryRegionOps macio_nvram_ops = {
  83. .read = macio_nvram_readb,
  84. .write = macio_nvram_writeb,
  85. .endianness = DEVICE_NATIVE_ENDIAN,
  86. };
  87. static const VMStateDescription vmstate_macio_nvram = {
  88. .name = "macio_nvram",
  89. .version_id = 1,
  90. .minimum_version_id = 1,
  91. .minimum_version_id_old = 1,
  92. .fields = (VMStateField[]) {
  93. VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size),
  94. VMSTATE_END_OF_LIST()
  95. }
  96. };
  97. static void macio_nvram_reset(void *opaque)
  98. {
  99. }
  100. MacIONVRAMState *macio_nvram_init (target_phys_addr_t size,
  101. unsigned int it_shift)
  102. {
  103. MacIONVRAMState *s;
  104. s = g_malloc0(sizeof(MacIONVRAMState));
  105. s->data = g_malloc0(size);
  106. s->size = size;
  107. s->it_shift = it_shift;
  108. memory_region_init_io(&s->mem, &macio_nvram_ops, s, "macio-nvram",
  109. size << it_shift);
  110. vmstate_register(NULL, -1, &vmstate_macio_nvram, s);
  111. qemu_register_reset(macio_nvram_reset, s);
  112. return s;
  113. }
  114. void macio_nvram_setup_bar(MacIONVRAMState *s, MemoryRegion *bar,
  115. target_phys_addr_t mem_base)
  116. {
  117. memory_region_add_subregion(bar, mem_base, &s->mem);
  118. }
  119. /* Set up a system OpenBIOS NVRAM partition */
  120. void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
  121. {
  122. unsigned int i;
  123. uint32_t start = 0, end;
  124. struct OpenBIOS_nvpart_v1 *part_header;
  125. // OpenBIOS nvram variables
  126. // Variable partition
  127. part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
  128. part_header->signature = OPENBIOS_PART_SYSTEM;
  129. pstrcpy(part_header->name, sizeof(part_header->name), "system");
  130. end = start + sizeof(struct OpenBIOS_nvpart_v1);
  131. for (i = 0; i < nb_prom_envs; i++)
  132. end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
  133. // End marker
  134. nvr->data[end++] = '\0';
  135. end = start + ((end - start + 15) & ~15);
  136. /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
  137. new variables. */
  138. if (end < DEF_SYSTEM_SIZE)
  139. end = DEF_SYSTEM_SIZE;
  140. OpenBIOS_finish_partition(part_header, end - start);
  141. // free partition
  142. start = end;
  143. part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
  144. part_header->signature = OPENBIOS_PART_FREE;
  145. pstrcpy(part_header->name, sizeof(part_header->name), "free");
  146. end = len;
  147. OpenBIOS_finish_partition(part_header, end - start);
  148. }