os-area.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. s64 rtc_diff;
  109. unsigned int av_multi_out;
  110. } static saved_params;
  111. #define dump_header(_a) _dump_header(_a, __func__, __LINE__)
  112. static void _dump_header(const struct os_area_header *h, const char *func,
  113. int line)
  114. {
  115. pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
  116. h->magic_num);
  117. pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
  118. h->hdr_version);
  119. pr_debug("%s:%d: h.os_area_offset: %u\n", func, line,
  120. h->os_area_offset);
  121. pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
  122. h->ldr_area_offset);
  123. pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
  124. h->ldr_format);
  125. pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
  126. h->ldr_size);
  127. }
  128. #define dump_params(_a) _dump_params(_a, __func__, __LINE__)
  129. static void _dump_params(const struct os_area_params *p, const char *func,
  130. int line)
  131. {
  132. pr_debug("%s:%d: p.boot_flag: %u\n", func, line, p->boot_flag);
  133. pr_debug("%s:%d: p.num_params: %u\n", func, line, p->num_params);
  134. pr_debug("%s:%d: p.rtc_diff %ld\n", func, line, p->rtc_diff);
  135. pr_debug("%s:%d: p.av_multi_out %u\n", func, line, p->av_multi_out);
  136. pr_debug("%s:%d: p.ctrl_button: %u\n", func, line, p->ctrl_button);
  137. pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func, line,
  138. p->static_ip_addr[0], p->static_ip_addr[1],
  139. p->static_ip_addr[2], p->static_ip_addr[3]);
  140. pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func, line,
  141. p->network_mask[0], p->network_mask[1],
  142. p->network_mask[2], p->network_mask[3]);
  143. pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func, line,
  144. p->default_gateway[0], p->default_gateway[1],
  145. p->default_gateway[2], p->default_gateway[3]);
  146. pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func, line,
  147. p->dns_primary[0], p->dns_primary[1],
  148. p->dns_primary[2], p->dns_primary[3]);
  149. pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func, line,
  150. p->dns_secondary[0], p->dns_secondary[1],
  151. p->dns_secondary[2], p->dns_secondary[3]);
  152. }
  153. static int __init verify_header(const struct os_area_header *header)
  154. {
  155. if (memcmp(header->magic_num, "cell_ext_os_area", 16)) {
  156. pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
  157. return -1;
  158. }
  159. if (header->hdr_version < 1) {
  160. pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
  161. return -1;
  162. }
  163. if (header->os_area_offset > header->ldr_area_offset) {
  164. pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. /**
  170. * os_area_queue_work_handler - Asynchronous write handler.
  171. *
  172. * An asynchronous write for flash memory and the device tree. Do not
  173. * call directly, use os_area_queue_work().
  174. */
  175. static void os_area_queue_work_handler(struct work_struct *work)
  176. {
  177. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  178. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  179. }
  180. static void os_area_queue_work(void)
  181. {
  182. static DECLARE_WORK(q, os_area_queue_work_handler);
  183. wmb();
  184. schedule_work(&q);
  185. }
  186. /**
  187. * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
  188. *
  189. * For the convenience of the guest, the HV makes a copy of the os area in
  190. * flash to a high address in the boot memory region and then puts that RAM
  191. * address and the byte count into the repository for retreval by the guest.
  192. * We copy the data we want into a static variable and allow the memory setup
  193. * by the HV to be claimed by the lmb manager.
  194. */
  195. void __init ps3_os_area_save_params(void)
  196. {
  197. int result;
  198. u64 lpar_addr;
  199. unsigned int size;
  200. struct os_area_header *header;
  201. struct os_area_params *params;
  202. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  203. result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
  204. if (result) {
  205. pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
  206. __func__, __LINE__);
  207. return;
  208. }
  209. header = (struct os_area_header *)__va(lpar_addr);
  210. params = (struct os_area_params *)__va(lpar_addr
  211. + OS_AREA_SEGMENT_SIZE);
  212. result = verify_header(header);
  213. if (result) {
  214. pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
  215. dump_header(header);
  216. return;
  217. }
  218. dump_header(header);
  219. dump_params(params);
  220. saved_params.rtc_diff = params->rtc_diff;
  221. saved_params.av_multi_out = params->av_multi_out;
  222. memset(header, 0, sizeof(*header));
  223. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  224. }
  225. /**
  226. * ps3_os_area_rtc_diff - Returns the rtc diff value.
  227. */
  228. u64 ps3_os_area_rtc_diff(void)
  229. {
  230. return saved_params.rtc_diff ? saved_params.rtc_diff
  231. : SECONDS_FROM_1970_TO_2000;
  232. }
  233. /**
  234. * ps3_os_area_get_av_multi_out - Returns the default video mode.
  235. */
  236. enum ps3_param_av_multi_out ps3_os_area_get_av_multi_out(void)
  237. {
  238. return saved_params.av_multi_out;
  239. }
  240. EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out);