os-area.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 <linux/fs.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/ctype.h>
  26. #include <linux/lmb.h>
  27. #include <asm/prom.h>
  28. #include "platform.h"
  29. enum {
  30. OS_AREA_SEGMENT_SIZE = 0X200,
  31. };
  32. enum os_area_ldr_format {
  33. HEADER_LDR_FORMAT_RAW = 0,
  34. HEADER_LDR_FORMAT_GZIP = 1,
  35. };
  36. #define OS_AREA_HEADER_MAGIC_NUM "cell_ext_os_area"
  37. /**
  38. * struct os_area_header - os area header segment.
  39. * @magic_num: Always 'cell_ext_os_area'.
  40. * @hdr_version: Header format version number.
  41. * @db_area_offset: Starting segment number of other os database area.
  42. * @ldr_area_offset: Starting segment number of bootloader image area.
  43. * @ldr_format: HEADER_LDR_FORMAT flag.
  44. * @ldr_size: Size of bootloader image in bytes.
  45. *
  46. * Note that the docs refer to area offsets. These are offsets in units of
  47. * segments from the start of the os area (top of the header). These are
  48. * better thought of as segment numbers. The os area of the os area is
  49. * reserved for the os image.
  50. */
  51. struct os_area_header {
  52. u8 magic_num[16];
  53. u32 hdr_version;
  54. u32 db_area_offset;
  55. u32 ldr_area_offset;
  56. u32 _reserved_1;
  57. u32 ldr_format;
  58. u32 ldr_size;
  59. u32 _reserved_2[6];
  60. };
  61. enum os_area_boot_flag {
  62. PARAM_BOOT_FLAG_GAME_OS = 0,
  63. PARAM_BOOT_FLAG_OTHER_OS = 1,
  64. };
  65. enum os_area_ctrl_button {
  66. PARAM_CTRL_BUTTON_O_IS_YES = 0,
  67. PARAM_CTRL_BUTTON_X_IS_YES = 1,
  68. };
  69. /**
  70. * struct os_area_params - os area params segment.
  71. * @boot_flag: User preference of operating system, PARAM_BOOT_FLAG flag.
  72. * @num_params: Number of params in this (params) segment.
  73. * @rtc_diff: Difference in seconds between 1970 and the ps3 rtc value.
  74. * @av_multi_out: User preference of AV output, PARAM_AV_MULTI_OUT flag.
  75. * @ctrl_button: User preference of controller button config, PARAM_CTRL_BUTTON
  76. * flag.
  77. * @static_ip_addr: User preference of static IP address.
  78. * @network_mask: User preference of static network mask.
  79. * @default_gateway: User preference of static default gateway.
  80. * @dns_primary: User preference of static primary dns server.
  81. * @dns_secondary: User preference of static secondary dns server.
  82. *
  83. * The ps3 rtc maintains a read-only value that approximates seconds since
  84. * 2000-01-01 00:00:00 UTC.
  85. *
  86. * User preference of zero for static_ip_addr means use dhcp.
  87. */
  88. struct os_area_params {
  89. u32 boot_flag;
  90. u32 _reserved_1[3];
  91. u32 num_params;
  92. u32 _reserved_2[3];
  93. /* param 0 */
  94. s64 rtc_diff;
  95. u8 av_multi_out;
  96. u8 ctrl_button;
  97. u8 _reserved_3[6];
  98. /* param 1 */
  99. u8 static_ip_addr[4];
  100. u8 network_mask[4];
  101. u8 default_gateway[4];
  102. u8 _reserved_4[4];
  103. /* param 2 */
  104. u8 dns_primary[4];
  105. u8 dns_secondary[4];
  106. u8 _reserved_5[8];
  107. };
  108. #define OS_AREA_DB_MAGIC_NUM "-db-"
  109. /**
  110. * struct os_area_db - Shared flash memory database.
  111. * @magic_num: Always '-db-'.
  112. * @version: os_area_db format version number.
  113. * @index_64: byte offset of the database id index for 64 bit variables.
  114. * @count_64: number of usable 64 bit index entries
  115. * @index_32: byte offset of the database id index for 32 bit variables.
  116. * @count_32: number of usable 32 bit index entries
  117. * @index_16: byte offset of the database id index for 16 bit variables.
  118. * @count_16: number of usable 16 bit index entries
  119. *
  120. * Flash rom storage for exclusive use by guests running in the other os lpar.
  121. * The current system configuration allocates 1K (two segments) for other os
  122. * use.
  123. */
  124. struct os_area_db {
  125. u8 magic_num[4];
  126. u16 version;
  127. u16 _reserved_1;
  128. u16 index_64;
  129. u16 count_64;
  130. u16 index_32;
  131. u16 count_32;
  132. u16 index_16;
  133. u16 count_16;
  134. u32 _reserved_2;
  135. u8 _db_data[1000];
  136. };
  137. /**
  138. * enum os_area_db_owner - Data owners.
  139. */
  140. enum os_area_db_owner {
  141. OS_AREA_DB_OWNER_ANY = -1,
  142. OS_AREA_DB_OWNER_NONE = 0,
  143. OS_AREA_DB_OWNER_PROTOTYPE = 1,
  144. OS_AREA_DB_OWNER_LINUX = 2,
  145. OS_AREA_DB_OWNER_PETITBOOT = 3,
  146. OS_AREA_DB_OWNER_MAX = 32,
  147. };
  148. enum os_area_db_key {
  149. OS_AREA_DB_KEY_ANY = -1,
  150. OS_AREA_DB_KEY_NONE = 0,
  151. OS_AREA_DB_KEY_RTC_DIFF = 1,
  152. OS_AREA_DB_KEY_VIDEO_MODE = 2,
  153. OS_AREA_DB_KEY_MAX = 8,
  154. };
  155. struct os_area_db_id {
  156. int owner;
  157. int key;
  158. };
  159. static const struct os_area_db_id os_area_db_id_empty = {
  160. .owner = OS_AREA_DB_OWNER_NONE,
  161. .key = OS_AREA_DB_KEY_NONE
  162. };
  163. static const struct os_area_db_id os_area_db_id_any = {
  164. .owner = OS_AREA_DB_OWNER_ANY,
  165. .key = OS_AREA_DB_KEY_ANY
  166. };
  167. static const struct os_area_db_id os_area_db_id_rtc_diff = {
  168. .owner = OS_AREA_DB_OWNER_LINUX,
  169. .key = OS_AREA_DB_KEY_RTC_DIFF
  170. };
  171. static const struct os_area_db_id os_area_db_id_video_mode = {
  172. .owner = OS_AREA_DB_OWNER_LINUX,
  173. .key = OS_AREA_DB_KEY_VIDEO_MODE
  174. };
  175. #define SECONDS_FROM_1970_TO_2000 946684800LL
  176. /**
  177. * struct saved_params - Static working copies of data from the PS3 'os area'.
  178. *
  179. * The order of preference we use for the rtc_diff source:
  180. * 1) The database value.
  181. * 2) The game os value.
  182. * 3) The number of seconds from 1970 to 2000.
  183. */
  184. struct saved_params {
  185. unsigned int valid;
  186. s64 rtc_diff;
  187. unsigned int av_multi_out;
  188. } static saved_params;
  189. static struct property property_rtc_diff = {
  190. .name = "linux,rtc_diff",
  191. .length = sizeof(saved_params.rtc_diff),
  192. .value = &saved_params.rtc_diff,
  193. };
  194. static struct property property_av_multi_out = {
  195. .name = "linux,av_multi_out",
  196. .length = sizeof(saved_params.av_multi_out),
  197. .value = &saved_params.av_multi_out,
  198. };
  199. /**
  200. * os_area_set_property - Add or overwrite a saved_params value to the device tree.
  201. *
  202. * Overwrites an existing property.
  203. */
  204. static void os_area_set_property(struct device_node *node,
  205. struct property *prop)
  206. {
  207. int result;
  208. struct property *tmp = of_find_property(node, prop->name, NULL);
  209. if (tmp) {
  210. pr_debug("%s:%d found %s\n", __func__, __LINE__, prop->name);
  211. prom_remove_property(node, tmp);
  212. }
  213. result = prom_add_property(node, prop);
  214. if (result)
  215. pr_debug("%s:%d prom_set_property failed\n", __func__,
  216. __LINE__);
  217. }
  218. /**
  219. * os_area_get_property - Get a saved_params value from the device tree.
  220. *
  221. */
  222. static void __init os_area_get_property(struct device_node *node,
  223. struct property *prop)
  224. {
  225. const struct property *tmp = of_find_property(node, prop->name, NULL);
  226. if (tmp) {
  227. BUG_ON(prop->length != tmp->length);
  228. memcpy(prop->value, tmp->value, prop->length);
  229. } else
  230. pr_debug("%s:%d not found %s\n", __func__, __LINE__,
  231. prop->name);
  232. }
  233. static void dump_field(char *s, const u8 *field, int size_of_field)
  234. {
  235. #if defined(DEBUG)
  236. int i;
  237. for (i = 0; i < size_of_field; i++)
  238. s[i] = isprint(field[i]) ? field[i] : '.';
  239. s[i] = 0;
  240. #endif
  241. }
  242. #define dump_header(_a) _dump_header(_a, __func__, __LINE__)
  243. static void _dump_header(const struct os_area_header *h, const char *func,
  244. int line)
  245. {
  246. char str[sizeof(h->magic_num) + 1];
  247. dump_field(str, h->magic_num, sizeof(h->magic_num));
  248. pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
  249. str);
  250. pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
  251. h->hdr_version);
  252. pr_debug("%s:%d: h.db_area_offset: %u\n", func, line,
  253. h->db_area_offset);
  254. pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
  255. h->ldr_area_offset);
  256. pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
  257. h->ldr_format);
  258. pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
  259. h->ldr_size);
  260. }
  261. #define dump_params(_a) _dump_params(_a, __func__, __LINE__)
  262. static void _dump_params(const struct os_area_params *p, const char *func,
  263. int line)
  264. {
  265. pr_debug("%s:%d: p.boot_flag: %u\n", func, line, p->boot_flag);
  266. pr_debug("%s:%d: p.num_params: %u\n", func, line, p->num_params);
  267. pr_debug("%s:%d: p.rtc_diff %ld\n", func, line, p->rtc_diff);
  268. pr_debug("%s:%d: p.av_multi_out %u\n", func, line, p->av_multi_out);
  269. pr_debug("%s:%d: p.ctrl_button: %u\n", func, line, p->ctrl_button);
  270. pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func, line,
  271. p->static_ip_addr[0], p->static_ip_addr[1],
  272. p->static_ip_addr[2], p->static_ip_addr[3]);
  273. pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func, line,
  274. p->network_mask[0], p->network_mask[1],
  275. p->network_mask[2], p->network_mask[3]);
  276. pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func, line,
  277. p->default_gateway[0], p->default_gateway[1],
  278. p->default_gateway[2], p->default_gateway[3]);
  279. pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func, line,
  280. p->dns_primary[0], p->dns_primary[1],
  281. p->dns_primary[2], p->dns_primary[3]);
  282. pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func, line,
  283. p->dns_secondary[0], p->dns_secondary[1],
  284. p->dns_secondary[2], p->dns_secondary[3]);
  285. }
  286. static int verify_header(const struct os_area_header *header)
  287. {
  288. if (memcmp(header->magic_num, OS_AREA_HEADER_MAGIC_NUM,
  289. sizeof(header->magic_num))) {
  290. pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
  291. return -1;
  292. }
  293. if (header->hdr_version < 1) {
  294. pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
  295. return -1;
  296. }
  297. if (header->db_area_offset > header->ldr_area_offset) {
  298. pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
  299. return -1;
  300. }
  301. return 0;
  302. }
  303. static int db_verify(const struct os_area_db *db)
  304. {
  305. if (memcmp(db->magic_num, OS_AREA_DB_MAGIC_NUM,
  306. sizeof(db->magic_num))) {
  307. pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
  308. return -1;
  309. }
  310. if (db->version != 1) {
  311. pr_debug("%s:%d version failed\n", __func__, __LINE__);
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. struct db_index {
  317. uint8_t owner:5;
  318. uint8_t key:3;
  319. };
  320. struct db_iterator {
  321. const struct os_area_db *db;
  322. struct os_area_db_id match_id;
  323. struct db_index *idx;
  324. struct db_index *last_idx;
  325. union {
  326. uint64_t *value_64;
  327. uint32_t *value_32;
  328. uint16_t *value_16;
  329. };
  330. };
  331. static unsigned int db_align_up(unsigned int val, unsigned int size)
  332. {
  333. return (val + (size - 1)) & (~(size - 1));
  334. }
  335. /**
  336. * db_for_each_64 - Iterator for 64 bit entries.
  337. *
  338. * A NULL value for id can be used to match all entries.
  339. * OS_AREA_DB_OWNER_ANY and OS_AREA_DB_KEY_ANY can be used to match all.
  340. */
  341. static int db_for_each_64(const struct os_area_db *db,
  342. const struct os_area_db_id *match_id, struct db_iterator *i)
  343. {
  344. next:
  345. if (!i->db) {
  346. i->db = db;
  347. i->match_id = match_id ? *match_id : os_area_db_id_any;
  348. i->idx = (void *)db + db->index_64;
  349. i->last_idx = i->idx + db->count_64;
  350. i->value_64 = (void *)db + db->index_64
  351. + db_align_up(db->count_64, 8);
  352. } else {
  353. i->idx++;
  354. i->value_64++;
  355. }
  356. if (i->idx >= i->last_idx) {
  357. pr_debug("%s:%d: reached end\n", __func__, __LINE__);
  358. return 0;
  359. }
  360. if (i->match_id.owner != OS_AREA_DB_OWNER_ANY
  361. && i->match_id.owner != (int)i->idx->owner)
  362. goto next;
  363. if (i->match_id.key != OS_AREA_DB_KEY_ANY
  364. && i->match_id.key != (int)i->idx->key)
  365. goto next;
  366. return 1;
  367. }
  368. static int db_delete_64(struct os_area_db *db, const struct os_area_db_id *id)
  369. {
  370. struct db_iterator i;
  371. for (i.db = NULL; db_for_each_64(db, id, &i); ) {
  372. pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__, __LINE__,
  373. i.idx->owner, i.idx->key,
  374. (unsigned long long)*i.value_64);
  375. i.idx->owner = 0;
  376. i.idx->key = 0;
  377. *i.value_64 = 0;
  378. }
  379. return 0;
  380. }
  381. static int db_set_64(struct os_area_db *db, const struct os_area_db_id *id,
  382. uint64_t value)
  383. {
  384. struct db_iterator i;
  385. pr_debug("%s:%d: (%d:%d) <= %llxh\n", __func__, __LINE__,
  386. id->owner, id->key, (unsigned long long)value);
  387. if (!id->owner || id->owner == OS_AREA_DB_OWNER_ANY
  388. || id->key == OS_AREA_DB_KEY_ANY) {
  389. pr_debug("%s:%d: bad id: (%d:%d)\n", __func__,
  390. __LINE__, id->owner, id->key);
  391. return -1;
  392. }
  393. db_delete_64(db, id);
  394. i.db = NULL;
  395. if (db_for_each_64(db, &os_area_db_id_empty, &i)) {
  396. pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__, __LINE__,
  397. i.idx->owner, i.idx->key,
  398. (unsigned long long)*i.value_64);
  399. i.idx->owner = id->owner;
  400. i.idx->key = id->key;
  401. *i.value_64 = value;
  402. pr_debug("%s:%d: set (%d:%d) <= %llxh\n", __func__, __LINE__,
  403. i.idx->owner, i.idx->key,
  404. (unsigned long long)*i.value_64);
  405. return 0;
  406. }
  407. pr_debug("%s:%d: database full.\n",
  408. __func__, __LINE__);
  409. return -1;
  410. }
  411. static int db_get_64(const struct os_area_db *db,
  412. const struct os_area_db_id *id, uint64_t *value)
  413. {
  414. struct db_iterator i;
  415. i.db = NULL;
  416. if (db_for_each_64(db, id, &i)) {
  417. *value = *i.value_64;
  418. pr_debug("%s:%d: found %lld\n", __func__, __LINE__,
  419. (long long int)*i.value_64);
  420. return 0;
  421. }
  422. pr_debug("%s:%d: not found\n", __func__, __LINE__);
  423. return -1;
  424. }
  425. static int db_get_rtc_diff(const struct os_area_db *db, int64_t *rtc_diff)
  426. {
  427. return db_get_64(db, &os_area_db_id_rtc_diff, (uint64_t*)rtc_diff);
  428. }
  429. #define dump_db(a) _dump_db(a, __func__, __LINE__)
  430. static void _dump_db(const struct os_area_db *db, const char *func,
  431. int line)
  432. {
  433. char str[sizeof(db->magic_num) + 1];
  434. dump_field(str, db->magic_num, sizeof(db->magic_num));
  435. pr_debug("%s:%d: db.magic_num: '%s'\n", func, line,
  436. str);
  437. pr_debug("%s:%d: db.version: %u\n", func, line,
  438. db->version);
  439. pr_debug("%s:%d: db.index_64: %u\n", func, line,
  440. db->index_64);
  441. pr_debug("%s:%d: db.count_64: %u\n", func, line,
  442. db->count_64);
  443. pr_debug("%s:%d: db.index_32: %u\n", func, line,
  444. db->index_32);
  445. pr_debug("%s:%d: db.count_32: %u\n", func, line,
  446. db->count_32);
  447. pr_debug("%s:%d: db.index_16: %u\n", func, line,
  448. db->index_16);
  449. pr_debug("%s:%d: db.count_16: %u\n", func, line,
  450. db->count_16);
  451. }
  452. static void os_area_db_init(struct os_area_db *db)
  453. {
  454. enum {
  455. HEADER_SIZE = offsetof(struct os_area_db, _db_data),
  456. INDEX_64_COUNT = 64,
  457. VALUES_64_COUNT = 57,
  458. INDEX_32_COUNT = 64,
  459. VALUES_32_COUNT = 57,
  460. INDEX_16_COUNT = 64,
  461. VALUES_16_COUNT = 57,
  462. };
  463. memset(db, 0, sizeof(struct os_area_db));
  464. memcpy(db->magic_num, OS_AREA_DB_MAGIC_NUM, sizeof(db->magic_num));
  465. db->version = 1;
  466. db->index_64 = HEADER_SIZE;
  467. db->count_64 = VALUES_64_COUNT;
  468. db->index_32 = HEADER_SIZE
  469. + INDEX_64_COUNT * sizeof(struct db_index)
  470. + VALUES_64_COUNT * sizeof(u64);
  471. db->count_32 = VALUES_32_COUNT;
  472. db->index_16 = HEADER_SIZE
  473. + INDEX_64_COUNT * sizeof(struct db_index)
  474. + VALUES_64_COUNT * sizeof(u64)
  475. + INDEX_32_COUNT * sizeof(struct db_index)
  476. + VALUES_32_COUNT * sizeof(u32);
  477. db->count_16 = VALUES_16_COUNT;
  478. /* Rules to check db layout. */
  479. BUILD_BUG_ON(sizeof(struct db_index) != 1);
  480. BUILD_BUG_ON(sizeof(struct os_area_db) != 2 * OS_AREA_SEGMENT_SIZE);
  481. BUILD_BUG_ON(INDEX_64_COUNT & 0x7);
  482. BUILD_BUG_ON(VALUES_64_COUNT > INDEX_64_COUNT);
  483. BUILD_BUG_ON(INDEX_32_COUNT & 0x7);
  484. BUILD_BUG_ON(VALUES_32_COUNT > INDEX_32_COUNT);
  485. BUILD_BUG_ON(INDEX_16_COUNT & 0x7);
  486. BUILD_BUG_ON(VALUES_16_COUNT > INDEX_16_COUNT);
  487. BUILD_BUG_ON(HEADER_SIZE
  488. + INDEX_64_COUNT * sizeof(struct db_index)
  489. + VALUES_64_COUNT * sizeof(u64)
  490. + INDEX_32_COUNT * sizeof(struct db_index)
  491. + VALUES_32_COUNT * sizeof(u32)
  492. + INDEX_16_COUNT * sizeof(struct db_index)
  493. + VALUES_16_COUNT * sizeof(u16)
  494. > sizeof(struct os_area_db));
  495. }
  496. /**
  497. * update_flash_db - Helper for os_area_queue_work_handler.
  498. *
  499. */
  500. static void update_flash_db(void)
  501. {
  502. int result;
  503. int file;
  504. off_t offset;
  505. ssize_t count;
  506. static const unsigned int buf_len = 8 * OS_AREA_SEGMENT_SIZE;
  507. const struct os_area_header *header;
  508. struct os_area_db* db;
  509. /* Read in header and db from flash. */
  510. file = sys_open("/dev/ps3flash", O_RDWR, 0);
  511. if (file < 0) {
  512. pr_debug("%s:%d sys_open failed\n", __func__, __LINE__);
  513. goto fail_open;
  514. }
  515. header = kmalloc(buf_len, GFP_KERNEL);
  516. if (!header) {
  517. pr_debug("%s:%d kmalloc failed\n", __func__, __LINE__);
  518. goto fail_malloc;
  519. }
  520. offset = sys_lseek(file, 0, SEEK_SET);
  521. if (offset != 0) {
  522. pr_debug("%s:%d sys_lseek failed\n", __func__, __LINE__);
  523. goto fail_header_seek;
  524. }
  525. count = sys_read(file, (char __user *)header, buf_len);
  526. result = count < OS_AREA_SEGMENT_SIZE || verify_header(header)
  527. || count < header->db_area_offset * OS_AREA_SEGMENT_SIZE;
  528. if (result) {
  529. pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
  530. dump_header(header);
  531. goto fail_header;
  532. }
  533. /* Now got a good db offset and some maybe good db data. */
  534. db = (void*)header + header->db_area_offset * OS_AREA_SEGMENT_SIZE;
  535. result = db_verify(db);
  536. if (result) {
  537. printk(KERN_NOTICE "%s:%d: Verify of flash database failed, "
  538. "formatting.\n", __func__, __LINE__);
  539. dump_db(db);
  540. os_area_db_init(db);
  541. }
  542. /* Now got good db data. */
  543. db_set_64(db, &os_area_db_id_rtc_diff, saved_params.rtc_diff);
  544. offset = sys_lseek(file, header->db_area_offset * OS_AREA_SEGMENT_SIZE,
  545. SEEK_SET);
  546. if (offset != header->db_area_offset * OS_AREA_SEGMENT_SIZE) {
  547. pr_debug("%s:%d sys_lseek failed\n", __func__, __LINE__);
  548. goto fail_db_seek;
  549. }
  550. count = sys_write(file, (const char __user *)db,
  551. sizeof(struct os_area_db));
  552. if (count < sizeof(struct os_area_db)) {
  553. pr_debug("%s:%d sys_write failed\n", __func__, __LINE__);
  554. }
  555. fail_db_seek:
  556. fail_header:
  557. fail_header_seek:
  558. kfree(header);
  559. fail_malloc:
  560. sys_close(file);
  561. fail_open:
  562. return;
  563. }
  564. /**
  565. * os_area_queue_work_handler - Asynchronous write handler.
  566. *
  567. * An asynchronous write for flash memory and the device tree. Do not
  568. * call directly, use os_area_queue_work().
  569. */
  570. static void os_area_queue_work_handler(struct work_struct *work)
  571. {
  572. struct device_node *node;
  573. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  574. node = of_find_node_by_path("/");
  575. if (node) {
  576. os_area_set_property(node, &property_rtc_diff);
  577. of_node_put(node);
  578. } else
  579. pr_debug("%s:%d of_find_node_by_path failed\n",
  580. __func__, __LINE__);
  581. #if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
  582. update_flash_db();
  583. #else
  584. printk(KERN_WARNING "%s:%d: No flash rom driver configured.\n",
  585. __func__, __LINE__);
  586. #endif
  587. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  588. }
  589. static void os_area_queue_work(void)
  590. {
  591. static DECLARE_WORK(q, os_area_queue_work_handler);
  592. wmb();
  593. schedule_work(&q);
  594. }
  595. /**
  596. * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
  597. *
  598. * For the convenience of the guest the HV makes a copy of the os area in
  599. * flash to a high address in the boot memory region and then puts that RAM
  600. * address and the byte count into the repository for retrieval by the guest.
  601. * We copy the data we want into a static variable and allow the memory setup
  602. * by the HV to be claimed by the lmb manager.
  603. *
  604. * The os area mirror will not be available to a second stage kernel, and
  605. * the header verify will fail. In this case, the saved_params values will
  606. * be set from flash memory or the passed in device tree in ps3_os_area_init().
  607. */
  608. void __init ps3_os_area_save_params(void)
  609. {
  610. int result;
  611. u64 lpar_addr;
  612. unsigned int size;
  613. struct os_area_header *header;
  614. struct os_area_params *params;
  615. struct os_area_db *db;
  616. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  617. result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
  618. if (result) {
  619. pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
  620. __func__, __LINE__);
  621. return;
  622. }
  623. header = (struct os_area_header *)__va(lpar_addr);
  624. params = (struct os_area_params *)__va(lpar_addr
  625. + OS_AREA_SEGMENT_SIZE);
  626. result = verify_header(header);
  627. if (result) {
  628. /* Second stage kernels exit here. */
  629. pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
  630. dump_header(header);
  631. return;
  632. }
  633. db = (struct os_area_db *)__va(lpar_addr
  634. + header->db_area_offset * OS_AREA_SEGMENT_SIZE);
  635. dump_header(header);
  636. dump_params(params);
  637. dump_db(db);
  638. result = db_verify(db) || db_get_rtc_diff(db, &saved_params.rtc_diff);
  639. if (result)
  640. saved_params.rtc_diff = params->rtc_diff ? params->rtc_diff
  641. : SECONDS_FROM_1970_TO_2000;
  642. saved_params.av_multi_out = params->av_multi_out;
  643. saved_params.valid = 1;
  644. memset(header, 0, sizeof(*header));
  645. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  646. }
  647. /**
  648. * ps3_os_area_init - Setup os area device tree properties as needed.
  649. */
  650. void __init ps3_os_area_init(void)
  651. {
  652. struct device_node *node;
  653. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  654. node = of_find_node_by_path("/");
  655. if (!saved_params.valid && node) {
  656. /* Second stage kernels should have a dt entry. */
  657. os_area_get_property(node, &property_rtc_diff);
  658. os_area_get_property(node, &property_av_multi_out);
  659. }
  660. if(!saved_params.rtc_diff)
  661. saved_params.rtc_diff = SECONDS_FROM_1970_TO_2000;
  662. if (node) {
  663. os_area_set_property(node, &property_rtc_diff);
  664. os_area_set_property(node, &property_av_multi_out);
  665. of_node_put(node);
  666. } else
  667. pr_debug("%s:%d of_find_node_by_path failed\n",
  668. __func__, __LINE__);
  669. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  670. }
  671. /**
  672. * ps3_os_area_get_rtc_diff - Returns the rtc diff value.
  673. */
  674. u64 ps3_os_area_get_rtc_diff(void)
  675. {
  676. return saved_params.rtc_diff;
  677. }
  678. /**
  679. * ps3_os_area_set_rtc_diff - Set the rtc diff value.
  680. *
  681. * An asynchronous write is needed to support writing updates from
  682. * the timer interrupt context.
  683. */
  684. void ps3_os_area_set_rtc_diff(u64 rtc_diff)
  685. {
  686. if (saved_params.rtc_diff != rtc_diff) {
  687. saved_params.rtc_diff = rtc_diff;
  688. os_area_queue_work();
  689. }
  690. }
  691. /**
  692. * ps3_os_area_get_av_multi_out - Returns the default video mode.
  693. */
  694. enum ps3_param_av_multi_out ps3_os_area_get_av_multi_out(void)
  695. {
  696. return saved_params.av_multi_out;
  697. }
  698. EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out);