os-area.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * PS3 'Other OS' area data.
  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 <asm/lmb.h>
  23. #include <asm/ps3.h>
  24. #include "platform.h"
  25. enum {
  26. OS_AREA_SEGMENT_SIZE = 0X200,
  27. };
  28. enum {
  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. s8 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. } __attribute__ ((packed));
  56. enum {
  57. PARAM_BOOT_FLAG_GAME_OS = 0,
  58. PARAM_BOOT_FLAG_OTHER_OS = 1,
  59. };
  60. enum {
  61. PARAM_AV_MULTI_OUT_NTSC = 0,
  62. PARAM_AV_MULTI_OUT_PAL_RGB = 1,
  63. PARAM_AV_MULTI_OUT_PAL_YCBCR = 2,
  64. PARAM_AV_MULTI_OUT_SECAM = 3,
  65. };
  66. enum {
  67. PARAM_CTRL_BUTTON_O_IS_YES = 0,
  68. PARAM_CTRL_BUTTON_X_IS_YES = 1,
  69. };
  70. /**
  71. * struct os_area_params - os area params segment.
  72. * @boot_flag: User preference of operating system, PARAM_BOOT_FLAG flag.
  73. * @num_params: Number of params in this (params) segment.
  74. * @rtc_diff: Difference in seconds between 1970 and the ps3 rtc value.
  75. * @av_multi_out: User preference of AV output, PARAM_AV_MULTI_OUT flag.
  76. * @ctrl_button: User preference of controller button config, PARAM_CTRL_BUTTON
  77. * flag.
  78. * @static_ip_addr: User preference of static IP address.
  79. * @network_mask: User preference of static network mask.
  80. * @default_gateway: User preference of static default gateway.
  81. * @dns_primary: User preference of static primary dns server.
  82. * @dns_secondary: User preference of static secondary dns server.
  83. *
  84. * User preference of zero for static_ip_addr means use dhcp.
  85. */
  86. struct os_area_params {
  87. u32 boot_flag;
  88. u32 _reserved_1[3];
  89. u32 num_params;
  90. u32 _reserved_2[3];
  91. /* param 0 */
  92. s64 rtc_diff;
  93. u8 av_multi_out;
  94. u8 ctrl_button;
  95. u8 _reserved_3[6];
  96. /* param 1 */
  97. u8 static_ip_addr[4];
  98. u8 network_mask[4];
  99. u8 default_gateway[4];
  100. u8 _reserved_4[4];
  101. /* param 2 */
  102. u8 dns_primary[4];
  103. u8 dns_secondary[4];
  104. u8 _reserved_5[8];
  105. } __attribute__ ((packed));
  106. /**
  107. * struct saved_params - Static working copies of data from the 'Other OS' area.
  108. *
  109. * For the convinience of the guest, the HV makes a copy of the 'Other OS' area
  110. * in flash to a high address in the boot memory region and then puts that RAM
  111. * address and the byte count into the repository for retreval by the guest.
  112. * We copy the data we want into a static variable and allow the memory setup
  113. * by the HV to be claimed by the lmb manager.
  114. */
  115. struct saved_params {
  116. /* param 0 */
  117. s64 rtc_diff;
  118. unsigned int av_multi_out;
  119. unsigned int ctrl_button;
  120. /* param 1 */
  121. u8 static_ip_addr[4];
  122. u8 network_mask[4];
  123. u8 default_gateway[4];
  124. /* param 2 */
  125. u8 dns_primary[4];
  126. u8 dns_secondary[4];
  127. } static saved_params;
  128. #define dump_header(_a) _dump_header(_a, __func__, __LINE__)
  129. static void _dump_header(const struct os_area_header __iomem *h, const char* func,
  130. int line)
  131. {
  132. pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
  133. h->magic_num);
  134. pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
  135. h->hdr_version);
  136. pr_debug("%s:%d: h.os_area_offset: %u\n", func, line,
  137. h->os_area_offset);
  138. pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
  139. h->ldr_area_offset);
  140. pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
  141. h->ldr_format);
  142. pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
  143. h->ldr_size);
  144. }
  145. #define dump_params(_a) _dump_params(_a, __func__, __LINE__)
  146. static void _dump_params(const struct os_area_params __iomem *p, const char* func,
  147. int line)
  148. {
  149. pr_debug("%s:%d: p.boot_flag: %u\n", func, line, p->boot_flag);
  150. pr_debug("%s:%d: p.num_params: %u\n", func, line, p->num_params);
  151. pr_debug("%s:%d: p.rtc_diff %ld\n", func, line, p->rtc_diff);
  152. pr_debug("%s:%d: p.av_multi_out %u\n", func, line, p->av_multi_out);
  153. pr_debug("%s:%d: p.ctrl_button: %u\n", func, line, p->ctrl_button);
  154. pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func, line,
  155. p->static_ip_addr[0], p->static_ip_addr[1],
  156. p->static_ip_addr[2], p->static_ip_addr[3]);
  157. pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func, line,
  158. p->network_mask[0], p->network_mask[1],
  159. p->network_mask[2], p->network_mask[3]);
  160. pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func, line,
  161. p->default_gateway[0], p->default_gateway[1],
  162. p->default_gateway[2], p->default_gateway[3]);
  163. pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func, line,
  164. p->dns_primary[0], p->dns_primary[1],
  165. p->dns_primary[2], p->dns_primary[3]);
  166. pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func, line,
  167. p->dns_secondary[0], p->dns_secondary[1],
  168. p->dns_secondary[2], p->dns_secondary[3]);
  169. }
  170. static int __init verify_header(const struct os_area_header *header)
  171. {
  172. if (memcmp(header->magic_num, "cell_ext_os_area", 16)) {
  173. pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
  174. return -1;
  175. }
  176. if (header->hdr_version < 1) {
  177. pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
  178. return -1;
  179. }
  180. if (header->os_area_offset > header->ldr_area_offset) {
  181. pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. int __init ps3_os_area_init(void)
  187. {
  188. int result;
  189. u64 lpar_addr;
  190. unsigned int size;
  191. struct os_area_header *header;
  192. struct os_area_params *params;
  193. result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
  194. if (result) {
  195. pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
  196. __func__, __LINE__);
  197. return result;
  198. }
  199. header = (struct os_area_header *)__va(lpar_addr);
  200. params = (struct os_area_params *)__va(lpar_addr + OS_AREA_SEGMENT_SIZE);
  201. result = verify_header(header);
  202. if (result) {
  203. pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
  204. dump_header(header);
  205. return -EIO;
  206. }
  207. dump_header(header);
  208. dump_params(params);
  209. saved_params.rtc_diff = params->rtc_diff;
  210. saved_params.av_multi_out = params->av_multi_out;
  211. saved_params.ctrl_button = params->ctrl_button;
  212. memcpy(saved_params.static_ip_addr, params->static_ip_addr, 4);
  213. memcpy(saved_params.network_mask, params->network_mask, 4);
  214. memcpy(saved_params.default_gateway, params->default_gateway, 4);
  215. memcpy(saved_params.dns_secondary, params->dns_secondary, 4);
  216. return result;
  217. }
  218. /**
  219. * ps3_os_area_rtc_diff - Returns the ps3 rtc diff value.
  220. *
  221. * The ps3 rtc maintains a value that approximates seconds since
  222. * 2000-01-01 00:00:00 UTC. Returns the exact number of seconds from 1970 to
  223. * 2000 when saved_params.rtc_diff has not been properly set up.
  224. */
  225. u64 ps3_os_area_rtc_diff(void)
  226. {
  227. return saved_params.rtc_diff ? saved_params.rtc_diff : 946684800UL;
  228. }