os-area.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * PS3 flash memory os area.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/io.h>
  22. #include <linux/workqueue.h>
  23. #include <asm/lmb.h>
  24. #include "platform.h"
  25. enum {
  26. OS_AREA_SEGMENT_SIZE = 0X200,
  27. };
  28. enum os_area_ldr_format {
  29. HEADER_LDR_FORMAT_RAW = 0,
  30. HEADER_LDR_FORMAT_GZIP = 1,
  31. };
  32. /**
  33. * struct os_area_header - os area header segment.
  34. * @magic_num: Always 'cell_ext_os_area'.
  35. * @hdr_version: Header format version number.
  36. * @os_area_offset: Starting segment number of os image area.
  37. * @ldr_area_offset: Starting segment number of bootloader image area.
  38. * @ldr_format: HEADER_LDR_FORMAT flag.
  39. * @ldr_size: Size of bootloader image in bytes.
  40. *
  41. * Note that the docs refer to area offsets. These are offsets in units of
  42. * segments from the start of the os area (top of the header). These are
  43. * better thought of as segment numbers. The os area of the os area is
  44. * reserved for the os image.
  45. */
  46. struct os_area_header {
  47. u8 magic_num[16];
  48. u32 hdr_version;
  49. u32 os_area_offset;
  50. u32 ldr_area_offset;
  51. u32 _reserved_1;
  52. u32 ldr_format;
  53. u32 ldr_size;
  54. u32 _reserved_2[6];
  55. };
  56. enum os_area_boot_flag {
  57. PARAM_BOOT_FLAG_GAME_OS = 0,
  58. PARAM_BOOT_FLAG_OTHER_OS = 1,
  59. };
  60. enum os_area_ctrl_button {
  61. PARAM_CTRL_BUTTON_O_IS_YES = 0,
  62. PARAM_CTRL_BUTTON_X_IS_YES = 1,
  63. };
  64. /**
  65. * struct os_area_params - os area params segment.
  66. * @boot_flag: User preference of operating system, PARAM_BOOT_FLAG flag.
  67. * @num_params: Number of params in this (params) segment.
  68. * @rtc_diff: Difference in seconds between 1970 and the ps3 rtc value.
  69. * @av_multi_out: User preference of AV output, PARAM_AV_MULTI_OUT flag.
  70. * @ctrl_button: User preference of controller button config, PARAM_CTRL_BUTTON
  71. * flag.
  72. * @static_ip_addr: User preference of static IP address.
  73. * @network_mask: User preference of static network mask.
  74. * @default_gateway: User preference of static default gateway.
  75. * @dns_primary: User preference of static primary dns server.
  76. * @dns_secondary: User preference of static secondary dns server.
  77. *
  78. * The ps3 rtc maintains a read-only value that approximates seconds since
  79. * 2000-01-01 00:00:00 UTC.
  80. *
  81. * User preference of zero for static_ip_addr means use dhcp.
  82. */
  83. struct os_area_params {
  84. u32 boot_flag;
  85. u32 _reserved_1[3];
  86. u32 num_params;
  87. u32 _reserved_2[3];
  88. /* param 0 */
  89. s64 rtc_diff;
  90. u8 av_multi_out;
  91. u8 ctrl_button;
  92. u8 _reserved_3[6];
  93. /* param 1 */
  94. u8 static_ip_addr[4];
  95. u8 network_mask[4];
  96. u8 default_gateway[4];
  97. u8 _reserved_4[4];
  98. /* param 2 */
  99. u8 dns_primary[4];
  100. u8 dns_secondary[4];
  101. u8 _reserved_5[8];
  102. };
  103. #define SECONDS_FROM_1970_TO_2000 946684800LL
  104. /**
  105. * struct saved_params - Static working copies of data from the PS3 'os area'.
  106. */
  107. struct saved_params {
  108. unsigned int valid;
  109. s64 rtc_diff;
  110. unsigned int av_multi_out;
  111. } static saved_params;
  112. static struct property property_rtc_diff = {
  113. .name = "linux,rtc_diff",
  114. .length = sizeof(saved_params.rtc_diff),
  115. .value = &saved_params.rtc_diff,
  116. };
  117. static struct property property_av_multi_out = {
  118. .name = "linux,av_multi_out",
  119. .length = sizeof(saved_params.av_multi_out),
  120. .value = &saved_params.av_multi_out,
  121. };
  122. /**
  123. * os_area_set_property - Add or overwrite a saved_params value to the device tree.
  124. *
  125. * Overwrites an existing property.
  126. */
  127. static void os_area_set_property(struct device_node *node,
  128. struct property *prop)
  129. {
  130. int result;
  131. struct property *tmp = of_find_property(node, prop->name, NULL);
  132. if (tmp) {
  133. pr_debug("%s:%d found %s\n", __func__, __LINE__, prop->name);
  134. prom_remove_property(node, tmp);
  135. }
  136. result = prom_add_property(node, prop);
  137. if (result)
  138. pr_debug("%s:%d prom_set_property failed\n", __func__,
  139. __LINE__);
  140. }
  141. /**
  142. * os_area_get_property - Get a saved_params value from the device tree.
  143. *
  144. */
  145. static void __init os_area_get_property(struct device_node *node,
  146. struct property *prop)
  147. {
  148. const struct property *tmp = of_find_property(node, prop->name, NULL);
  149. if (tmp) {
  150. BUG_ON(prop->length != tmp->length);
  151. memcpy(prop->value, tmp->value, prop->length);
  152. } else
  153. pr_debug("%s:%d not found %s\n", __func__, __LINE__,
  154. prop->name);
  155. }
  156. #define dump_header(_a) _dump_header(_a, __func__, __LINE__)
  157. static void _dump_header(const struct os_area_header *h, const char *func,
  158. int line)
  159. {
  160. pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
  161. h->magic_num);
  162. pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
  163. h->hdr_version);
  164. pr_debug("%s:%d: h.os_area_offset: %u\n", func, line,
  165. h->os_area_offset);
  166. pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
  167. h->ldr_area_offset);
  168. pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
  169. h->ldr_format);
  170. pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
  171. h->ldr_size);
  172. }
  173. #define dump_params(_a) _dump_params(_a, __func__, __LINE__)
  174. static void _dump_params(const struct os_area_params *p, const char *func,
  175. int line)
  176. {
  177. pr_debug("%s:%d: p.boot_flag: %u\n", func, line, p->boot_flag);
  178. pr_debug("%s:%d: p.num_params: %u\n", func, line, p->num_params);
  179. pr_debug("%s:%d: p.rtc_diff %ld\n", func, line, p->rtc_diff);
  180. pr_debug("%s:%d: p.av_multi_out %u\n", func, line, p->av_multi_out);
  181. pr_debug("%s:%d: p.ctrl_button: %u\n", func, line, p->ctrl_button);
  182. pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func, line,
  183. p->static_ip_addr[0], p->static_ip_addr[1],
  184. p->static_ip_addr[2], p->static_ip_addr[3]);
  185. pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func, line,
  186. p->network_mask[0], p->network_mask[1],
  187. p->network_mask[2], p->network_mask[3]);
  188. pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func, line,
  189. p->default_gateway[0], p->default_gateway[1],
  190. p->default_gateway[2], p->default_gateway[3]);
  191. pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func, line,
  192. p->dns_primary[0], p->dns_primary[1],
  193. p->dns_primary[2], p->dns_primary[3]);
  194. pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func, line,
  195. p->dns_secondary[0], p->dns_secondary[1],
  196. p->dns_secondary[2], p->dns_secondary[3]);
  197. }
  198. static int __init verify_header(const struct os_area_header *header)
  199. {
  200. if (memcmp(header->magic_num, "cell_ext_os_area", 16)) {
  201. pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
  202. return -1;
  203. }
  204. if (header->hdr_version < 1) {
  205. pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
  206. return -1;
  207. }
  208. if (header->os_area_offset > header->ldr_area_offset) {
  209. pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. /**
  215. * os_area_queue_work_handler - Asynchronous write handler.
  216. *
  217. * An asynchronous write for flash memory and the device tree. Do not
  218. * call directly, use os_area_queue_work().
  219. */
  220. static void os_area_queue_work_handler(struct work_struct *work)
  221. {
  222. struct device_node *node;
  223. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  224. node = of_find_node_by_path("/");
  225. if (node) {
  226. os_area_set_property(node, &property_rtc_diff);
  227. of_node_put(node);
  228. } else
  229. pr_debug("%s:%d of_find_node_by_path failed\n",
  230. __func__, __LINE__);
  231. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  232. }
  233. static void os_area_queue_work(void)
  234. {
  235. static DECLARE_WORK(q, os_area_queue_work_handler);
  236. wmb();
  237. schedule_work(&q);
  238. }
  239. /**
  240. * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
  241. *
  242. * For the convenience of the guest, the HV makes a copy of the os area in
  243. * flash to a high address in the boot memory region and then puts that RAM
  244. * address and the byte count into the repository for retreval by the guest.
  245. * We copy the data we want into a static variable and allow the memory setup
  246. * by the HV to be claimed by the lmb manager.
  247. */
  248. void __init ps3_os_area_save_params(void)
  249. {
  250. int result;
  251. u64 lpar_addr;
  252. unsigned int size;
  253. struct os_area_header *header;
  254. struct os_area_params *params;
  255. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  256. result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
  257. if (result) {
  258. pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
  259. __func__, __LINE__);
  260. return;
  261. }
  262. header = (struct os_area_header *)__va(lpar_addr);
  263. params = (struct os_area_params *)__va(lpar_addr
  264. + OS_AREA_SEGMENT_SIZE);
  265. result = verify_header(header);
  266. if (result) {
  267. /* Second stage kernels exit here. */
  268. pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
  269. dump_header(header);
  270. return;
  271. }
  272. dump_header(header);
  273. dump_params(params);
  274. saved_params.rtc_diff = params->rtc_diff;
  275. saved_params.av_multi_out = params->av_multi_out;
  276. saved_params.valid = 1;
  277. memset(header, 0, sizeof(*header));
  278. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  279. }
  280. /**
  281. * ps3_os_area_init - Setup os area device tree properties as needed.
  282. */
  283. void __init ps3_os_area_init(void)
  284. {
  285. struct device_node *node;
  286. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  287. node = of_find_node_by_path("/");
  288. if (!saved_params.valid && node) {
  289. /* Second stage kernels should have a dt entry. */
  290. os_area_get_property(node, &property_rtc_diff);
  291. os_area_get_property(node, &property_av_multi_out);
  292. }
  293. if(!saved_params.rtc_diff)
  294. saved_params.rtc_diff = SECONDS_FROM_1970_TO_2000;
  295. if (node) {
  296. os_area_set_property(node, &property_rtc_diff);
  297. os_area_set_property(node, &property_av_multi_out);
  298. of_node_put(node);
  299. } else
  300. pr_debug("%s:%d of_find_node_by_path failed\n",
  301. __func__, __LINE__);
  302. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  303. }
  304. /**
  305. * ps3_os_area_get_rtc_diff - Returns the rtc diff value.
  306. */
  307. u64 ps3_os_area_get_rtc_diff(void)
  308. {
  309. return saved_params.rtc_diff;
  310. }
  311. /**
  312. * ps3_os_area_set_rtc_diff - Set the rtc diff value.
  313. *
  314. * An asynchronous write is needed to support writing updates from
  315. * the timer interrupt context.
  316. */
  317. void ps3_os_area_set_rtc_diff(u64 rtc_diff)
  318. {
  319. if (saved_params.rtc_diff != rtc_diff) {
  320. saved_params.rtc_diff = rtc_diff;
  321. os_area_queue_work();
  322. }
  323. }
  324. /**
  325. * ps3_os_area_get_av_multi_out - Returns the default video mode.
  326. */
  327. enum ps3_param_av_multi_out ps3_os_area_get_av_multi_out(void)
  328. {
  329. return saved_params.av_multi_out;
  330. }
  331. EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out);