cfg.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * VMApple Configuration Region
  3. *
  4. * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0-or-later
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  9. * See the COPYING file in the top-level directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/vmapple/vmapple.h"
  13. #include "hw/sysbus.h"
  14. #include "qemu/log.h"
  15. #include "qemu/module.h"
  16. #include "qapi/error.h"
  17. #include "net/net.h"
  18. OBJECT_DECLARE_SIMPLE_TYPE(VMAppleCfgState, VMAPPLE_CFG)
  19. #define VMAPPLE_CFG_SIZE 0x00010000
  20. typedef struct VMAppleCfg {
  21. uint32_t version; /* 0x000 */
  22. uint32_t nr_cpus; /* 0x004 */
  23. uint32_t unk1; /* 0x008 */
  24. uint32_t unk2; /* 0x00c */
  25. uint32_t unk3; /* 0x010 */
  26. uint32_t unk4; /* 0x014 */
  27. uint64_t ecid; /* 0x018 */
  28. uint64_t ram_size; /* 0x020 */
  29. uint32_t run_installer1; /* 0x028 */
  30. uint32_t unk5; /* 0x02c */
  31. uint32_t unk6; /* 0x030 */
  32. uint32_t run_installer2; /* 0x034 */
  33. uint32_t rnd; /* 0x038 */
  34. uint32_t unk7; /* 0x03c */
  35. MACAddr mac_en0; /* 0x040 */
  36. uint8_t pad1[2];
  37. MACAddr mac_en1; /* 0x048 */
  38. uint8_t pad2[2];
  39. MACAddr mac_wifi0; /* 0x050 */
  40. uint8_t pad3[2];
  41. MACAddr mac_bt0; /* 0x058 */
  42. uint8_t pad4[2];
  43. uint8_t reserved[0xa0]; /* 0x060 */
  44. uint32_t cpu_ids[0x80]; /* 0x100 */
  45. uint8_t scratch[0x200]; /* 0x180 */
  46. char serial[32]; /* 0x380 */
  47. char unk8[32]; /* 0x3a0 */
  48. char model[32]; /* 0x3c0 */
  49. uint8_t unk9[32]; /* 0x3e0 */
  50. uint32_t unk10; /* 0x400 */
  51. char soc_name[32]; /* 0x404 */
  52. } VMAppleCfg;
  53. struct VMAppleCfgState {
  54. SysBusDevice parent_obj;
  55. VMAppleCfg cfg;
  56. MemoryRegion mem;
  57. char *serial;
  58. char *model;
  59. char *soc_name;
  60. };
  61. static void vmapple_cfg_reset(Object *obj, ResetType type)
  62. {
  63. VMAppleCfgState *s = VMAPPLE_CFG(obj);
  64. VMAppleCfg *cfg;
  65. cfg = memory_region_get_ram_ptr(&s->mem);
  66. memset(cfg, 0, VMAPPLE_CFG_SIZE);
  67. *cfg = s->cfg;
  68. }
  69. static bool set_fixlen_property_or_error(char *restrict dst,
  70. const char *restrict src,
  71. size_t dst_size, Error **errp,
  72. const char *property_name)
  73. {
  74. ERRP_GUARD();
  75. size_t len;
  76. len = g_strlcpy(dst, src, dst_size);
  77. if (len < dst_size) { /* len does not count nul terminator */
  78. return true;
  79. }
  80. error_setg(errp, "Provided value too long for property '%s'", property_name);
  81. error_append_hint(errp, "length (%zu) exceeds maximum of %zu\n",
  82. len, dst_size - 1);
  83. return false;
  84. }
  85. #define set_fixlen_property_or_return(dst_array, src, errp, property_name) \
  86. do { \
  87. if (!set_fixlen_property_or_error((dst_array), (src), \
  88. ARRAY_SIZE(dst_array), \
  89. (errp), (property_name))) { \
  90. return; \
  91. } \
  92. } while (0)
  93. static void vmapple_cfg_realize(DeviceState *dev, Error **errp)
  94. {
  95. VMAppleCfgState *s = VMAPPLE_CFG(dev);
  96. uint32_t i;
  97. if (!s->serial) {
  98. s->serial = g_strdup("1234");
  99. }
  100. if (!s->model) {
  101. s->model = g_strdup("VM0001");
  102. }
  103. if (!s->soc_name) {
  104. s->soc_name = g_strdup("Apple M1 (Virtual)");
  105. }
  106. set_fixlen_property_or_return(s->cfg.serial, s->serial, errp, "serial");
  107. set_fixlen_property_or_return(s->cfg.model, s->model, errp, "model");
  108. set_fixlen_property_or_return(s->cfg.soc_name, s->soc_name, errp, "soc_name");
  109. set_fixlen_property_or_return(s->cfg.unk8, "D/A", errp, "unk8");
  110. s->cfg.version = 2;
  111. s->cfg.unk1 = 1;
  112. s->cfg.unk2 = 1;
  113. s->cfg.unk3 = 0x20;
  114. s->cfg.unk4 = 0;
  115. s->cfg.unk5 = 1;
  116. s->cfg.unk6 = 1;
  117. s->cfg.unk7 = 0;
  118. s->cfg.unk10 = 1;
  119. if (s->cfg.nr_cpus > ARRAY_SIZE(s->cfg.cpu_ids)) {
  120. error_setg(errp,
  121. "Failed to create %u CPUs, vmapple machine supports %zu max",
  122. s->cfg.nr_cpus, ARRAY_SIZE(s->cfg.cpu_ids));
  123. return;
  124. }
  125. for (i = 0; i < s->cfg.nr_cpus; i++) {
  126. s->cfg.cpu_ids[i] = i;
  127. }
  128. }
  129. static void vmapple_cfg_init(Object *obj)
  130. {
  131. VMAppleCfgState *s = VMAPPLE_CFG(obj);
  132. memory_region_init_ram(&s->mem, obj, "VMApple Config", VMAPPLE_CFG_SIZE,
  133. &error_fatal);
  134. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mem);
  135. }
  136. static const Property vmapple_cfg_properties[] = {
  137. DEFINE_PROP_UINT32("nr-cpus", VMAppleCfgState, cfg.nr_cpus, 1),
  138. DEFINE_PROP_UINT64("ecid", VMAppleCfgState, cfg.ecid, 0),
  139. DEFINE_PROP_UINT64("ram-size", VMAppleCfgState, cfg.ram_size, 0),
  140. DEFINE_PROP_UINT32("run_installer1", VMAppleCfgState, cfg.run_installer1, 0),
  141. DEFINE_PROP_UINT32("run_installer2", VMAppleCfgState, cfg.run_installer2, 0),
  142. DEFINE_PROP_UINT32("rnd", VMAppleCfgState, cfg.rnd, 0),
  143. DEFINE_PROP_MACADDR("mac-en0", VMAppleCfgState, cfg.mac_en0),
  144. DEFINE_PROP_MACADDR("mac-en1", VMAppleCfgState, cfg.mac_en1),
  145. DEFINE_PROP_MACADDR("mac-wifi0", VMAppleCfgState, cfg.mac_wifi0),
  146. DEFINE_PROP_MACADDR("mac-bt0", VMAppleCfgState, cfg.mac_bt0),
  147. DEFINE_PROP_STRING("serial", VMAppleCfgState, serial),
  148. DEFINE_PROP_STRING("model", VMAppleCfgState, model),
  149. DEFINE_PROP_STRING("soc_name", VMAppleCfgState, soc_name),
  150. };
  151. static void vmapple_cfg_class_init(ObjectClass *klass, void *data)
  152. {
  153. DeviceClass *dc = DEVICE_CLASS(klass);
  154. ResettableClass *rc = RESETTABLE_CLASS(klass);
  155. dc->realize = vmapple_cfg_realize;
  156. dc->desc = "VMApple Configuration Region";
  157. device_class_set_props(dc, vmapple_cfg_properties);
  158. rc->phases.hold = vmapple_cfg_reset;
  159. }
  160. static const TypeInfo vmapple_cfg_info = {
  161. .name = TYPE_VMAPPLE_CFG,
  162. .parent = TYPE_SYS_BUS_DEVICE,
  163. .instance_size = sizeof(VMAppleCfgState),
  164. .instance_init = vmapple_cfg_init,
  165. .class_init = vmapple_cfg_class_init,
  166. };
  167. static void vmapple_cfg_register_types(void)
  168. {
  169. type_register_static(&vmapple_cfg_info);
  170. }
  171. type_init(vmapple_cfg_register_types)