blob: b9ea09d9d2fb130056f59d59640eec8554623fa0 [file] [log] [blame]
Geoff Levand00a3e2e2006-11-23 00:46:58 +01001/*
Geoff Levandca942972007-10-07 07:35:43 +10002 * PS3 flash memory os area.
Geoff Levand00a3e2e2006-11-23 00:46:58 +01003 *
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
21#include <linux/kernel.h>
22#include <linux/io.h>
Geoff Levand418ef202007-10-07 07:35:45 +100023#include <linux/workqueue.h>
Geoff Levandef2ac632007-10-09 11:07:24 +100024#include <linux/fs.h>
25#include <linux/syscalls.h>
Geoff Levandec5d2df2007-12-24 04:41:01 +110026#include <linux/ctype.h>
Geoff Levand00a3e2e2006-11-23 00:46:58 +010027
28#include <asm/lmb.h>
Geoff Levand00a3e2e2006-11-23 00:46:58 +010029
30#include "platform.h"
31
32enum {
33 OS_AREA_SEGMENT_SIZE = 0X200,
34};
35
Geoff Levandca942972007-10-07 07:35:43 +100036enum os_area_ldr_format {
Geoff Levand00a3e2e2006-11-23 00:46:58 +010037 HEADER_LDR_FORMAT_RAW = 0,
38 HEADER_LDR_FORMAT_GZIP = 1,
39};
40
Geoff Levandec5d2df2007-12-24 04:41:01 +110041#define OS_AREA_HEADER_MAGIC_NUM "cell_ext_os_area"
42
Geoff Levand00a3e2e2006-11-23 00:46:58 +010043/**
44 * struct os_area_header - os area header segment.
45 * @magic_num: Always 'cell_ext_os_area'.
46 * @hdr_version: Header format version number.
Geoff Levandef2ac632007-10-09 11:07:24 +100047 * @db_area_offset: Starting segment number of other os database area.
Geoff Levand00a3e2e2006-11-23 00:46:58 +010048 * @ldr_area_offset: Starting segment number of bootloader image area.
49 * @ldr_format: HEADER_LDR_FORMAT flag.
50 * @ldr_size: Size of bootloader image in bytes.
51 *
52 * Note that the docs refer to area offsets. These are offsets in units of
53 * segments from the start of the os area (top of the header). These are
54 * better thought of as segment numbers. The os area of the os area is
55 * reserved for the os image.
56 */
57
58struct os_area_header {
Geoff Levandca942972007-10-07 07:35:43 +100059 u8 magic_num[16];
Geoff Levand00a3e2e2006-11-23 00:46:58 +010060 u32 hdr_version;
Geoff Levandef2ac632007-10-09 11:07:24 +100061 u32 db_area_offset;
Geoff Levand00a3e2e2006-11-23 00:46:58 +010062 u32 ldr_area_offset;
63 u32 _reserved_1;
64 u32 ldr_format;
65 u32 ldr_size;
66 u32 _reserved_2[6];
Geoff Levanda8229a92007-01-26 19:07:56 -080067};
Geoff Levand00a3e2e2006-11-23 00:46:58 +010068
Geoff Levandca942972007-10-07 07:35:43 +100069enum os_area_boot_flag {
Geoff Levand00a3e2e2006-11-23 00:46:58 +010070 PARAM_BOOT_FLAG_GAME_OS = 0,
71 PARAM_BOOT_FLAG_OTHER_OS = 1,
72};
73
Geoff Levandca942972007-10-07 07:35:43 +100074enum os_area_ctrl_button {
Geoff Levand00a3e2e2006-11-23 00:46:58 +010075 PARAM_CTRL_BUTTON_O_IS_YES = 0,
76 PARAM_CTRL_BUTTON_X_IS_YES = 1,
77};
78
79/**
80 * struct os_area_params - os area params segment.
81 * @boot_flag: User preference of operating system, PARAM_BOOT_FLAG flag.
82 * @num_params: Number of params in this (params) segment.
83 * @rtc_diff: Difference in seconds between 1970 and the ps3 rtc value.
84 * @av_multi_out: User preference of AV output, PARAM_AV_MULTI_OUT flag.
85 * @ctrl_button: User preference of controller button config, PARAM_CTRL_BUTTON
86 * flag.
87 * @static_ip_addr: User preference of static IP address.
88 * @network_mask: User preference of static network mask.
89 * @default_gateway: User preference of static default gateway.
90 * @dns_primary: User preference of static primary dns server.
91 * @dns_secondary: User preference of static secondary dns server.
92 *
Geoff Levandca942972007-10-07 07:35:43 +100093 * The ps3 rtc maintains a read-only value that approximates seconds since
94 * 2000-01-01 00:00:00 UTC.
95 *
Geoff Levand00a3e2e2006-11-23 00:46:58 +010096 * User preference of zero for static_ip_addr means use dhcp.
97 */
98
99struct os_area_params {
100 u32 boot_flag;
101 u32 _reserved_1[3];
102 u32 num_params;
103 u32 _reserved_2[3];
104 /* param 0 */
105 s64 rtc_diff;
106 u8 av_multi_out;
107 u8 ctrl_button;
108 u8 _reserved_3[6];
109 /* param 1 */
110 u8 static_ip_addr[4];
111 u8 network_mask[4];
112 u8 default_gateway[4];
113 u8 _reserved_4[4];
114 /* param 2 */
115 u8 dns_primary[4];
116 u8 dns_secondary[4];
117 u8 _reserved_5[8];
Geoff Levanda8229a92007-01-26 19:07:56 -0800118};
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100119
Geoff Levandec5d2df2007-12-24 04:41:01 +1100120#define OS_AREA_DB_MAGIC_NUM "-db-"
Geoff Levandef2ac632007-10-09 11:07:24 +1000121
122/**
123 * struct os_area_db - Shared flash memory database.
Geoff Levandec5d2df2007-12-24 04:41:01 +1100124 * @magic_num: Always '-db-'.
Geoff Levandef2ac632007-10-09 11:07:24 +1000125 * @version: os_area_db format version number.
126 * @index_64: byte offset of the database id index for 64 bit variables.
127 * @count_64: number of usable 64 bit index entries
128 * @index_32: byte offset of the database id index for 32 bit variables.
129 * @count_32: number of usable 32 bit index entries
130 * @index_16: byte offset of the database id index for 16 bit variables.
131 * @count_16: number of usable 16 bit index entries
132 *
133 * Flash rom storage for exclusive use by guests running in the other os lpar.
134 * The current system configuration allocates 1K (two segments) for other os
135 * use.
136 */
137
138struct os_area_db {
Geoff Levandec5d2df2007-12-24 04:41:01 +1100139 u8 magic_num[4];
Geoff Levandef2ac632007-10-09 11:07:24 +1000140 u16 version;
141 u16 _reserved_1;
142 u16 index_64;
143 u16 count_64;
144 u16 index_32;
145 u16 count_32;
146 u16 index_16;
147 u16 count_16;
148 u32 _reserved_2;
149 u8 _db_data[1000];
150};
151
152/**
153 * enum os_area_db_owner - Data owners.
154 */
155
156enum os_area_db_owner {
157 OS_AREA_DB_OWNER_ANY = -1,
158 OS_AREA_DB_OWNER_NONE = 0,
159 OS_AREA_DB_OWNER_PROTOTYPE = 1,
160 OS_AREA_DB_OWNER_LINUX = 2,
161 OS_AREA_DB_OWNER_PETITBOOT = 3,
162 OS_AREA_DB_OWNER_MAX = 32,
163};
164
165enum os_area_db_key {
166 OS_AREA_DB_KEY_ANY = -1,
167 OS_AREA_DB_KEY_NONE = 0,
168 OS_AREA_DB_KEY_RTC_DIFF = 1,
169 OS_AREA_DB_KEY_VIDEO_MODE = 2,
170 OS_AREA_DB_KEY_MAX = 8,
171};
172
173struct os_area_db_id {
174 int owner;
175 int key;
176};
177
178static const struct os_area_db_id os_area_db_id_empty = {
179 .owner = OS_AREA_DB_OWNER_NONE,
180 .key = OS_AREA_DB_KEY_NONE
181};
182
183static const struct os_area_db_id os_area_db_id_any = {
184 .owner = OS_AREA_DB_OWNER_ANY,
185 .key = OS_AREA_DB_KEY_ANY
186};
187
188static const struct os_area_db_id os_area_db_id_rtc_diff = {
189 .owner = OS_AREA_DB_OWNER_LINUX,
190 .key = OS_AREA_DB_KEY_RTC_DIFF
191};
192
193static const struct os_area_db_id os_area_db_id_video_mode = {
194 .owner = OS_AREA_DB_OWNER_LINUX,
195 .key = OS_AREA_DB_KEY_VIDEO_MODE
196};
197
Geoff Levandca942972007-10-07 07:35:43 +1000198#define SECONDS_FROM_1970_TO_2000 946684800LL
199
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100200/**
Geoff Levand01263e82007-10-07 07:35:44 +1000201 * struct saved_params - Static working copies of data from the PS3 'os area'.
Geoff Levandef2ac632007-10-09 11:07:24 +1000202 *
203 * The order of preference we use for the rtc_diff source:
204 * 1) The database value.
205 * 2) The game os value.
206 * 3) The number of seconds from 1970 to 2000.
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100207 */
208
209struct saved_params {
Geoff Levand7db19422007-10-07 07:35:47 +1000210 unsigned int valid;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100211 s64 rtc_diff;
212 unsigned int av_multi_out;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100213} static saved_params;
214
Geoff Levand7db19422007-10-07 07:35:47 +1000215static struct property property_rtc_diff = {
216 .name = "linux,rtc_diff",
217 .length = sizeof(saved_params.rtc_diff),
218 .value = &saved_params.rtc_diff,
219};
220
221static struct property property_av_multi_out = {
222 .name = "linux,av_multi_out",
223 .length = sizeof(saved_params.av_multi_out),
224 .value = &saved_params.av_multi_out,
225};
226
227/**
228 * os_area_set_property - Add or overwrite a saved_params value to the device tree.
229 *
230 * Overwrites an existing property.
231 */
232
233static void os_area_set_property(struct device_node *node,
234 struct property *prop)
235{
236 int result;
237 struct property *tmp = of_find_property(node, prop->name, NULL);
238
239 if (tmp) {
240 pr_debug("%s:%d found %s\n", __func__, __LINE__, prop->name);
241 prom_remove_property(node, tmp);
242 }
243
244 result = prom_add_property(node, prop);
245
246 if (result)
247 pr_debug("%s:%d prom_set_property failed\n", __func__,
248 __LINE__);
249}
250
251/**
252 * os_area_get_property - Get a saved_params value from the device tree.
253 *
254 */
255
256static void __init os_area_get_property(struct device_node *node,
257 struct property *prop)
258{
259 const struct property *tmp = of_find_property(node, prop->name, NULL);
260
261 if (tmp) {
262 BUG_ON(prop->length != tmp->length);
263 memcpy(prop->value, tmp->value, prop->length);
264 } else
265 pr_debug("%s:%d not found %s\n", __func__, __LINE__,
266 prop->name);
267}
268
Geoff Levandec5d2df2007-12-24 04:41:01 +1100269static void dump_field(char *s, const u8 *field, int size_of_field)
270{
271#if defined(DEBUG)
272 int i;
273
274 for (i = 0; i < size_of_field; i++)
275 s[i] = isprint(field[i]) ? field[i] : '.';
276 s[i] = 0;
277#endif
278}
279
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100280#define dump_header(_a) _dump_header(_a, __func__, __LINE__)
Geert Uytterhoeven670ad352007-06-16 07:19:04 +1000281static void _dump_header(const struct os_area_header *h, const char *func,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100282 int line)
283{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100284 char str[sizeof(h->magic_num) + 1];
285
286 dump_field(str, h->magic_num, sizeof(h->magic_num));
Geoff Levandef2ac632007-10-09 11:07:24 +1000287 pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
Geoff Levandec5d2df2007-12-24 04:41:01 +1100288 str);
Geoff Levandef2ac632007-10-09 11:07:24 +1000289 pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100290 h->hdr_version);
Geoff Levandef2ac632007-10-09 11:07:24 +1000291 pr_debug("%s:%d: h.db_area_offset: %u\n", func, line,
292 h->db_area_offset);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100293 pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
294 h->ldr_area_offset);
Geoff Levandef2ac632007-10-09 11:07:24 +1000295 pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100296 h->ldr_format);
Geoff Levandef2ac632007-10-09 11:07:24 +1000297 pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100298 h->ldr_size);
299}
300
301#define dump_params(_a) _dump_params(_a, __func__, __LINE__)
Geert Uytterhoeven670ad352007-06-16 07:19:04 +1000302static void _dump_params(const struct os_area_params *p, const char *func,
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100303 int line)
304{
305 pr_debug("%s:%d: p.boot_flag: %u\n", func, line, p->boot_flag);
306 pr_debug("%s:%d: p.num_params: %u\n", func, line, p->num_params);
307 pr_debug("%s:%d: p.rtc_diff %ld\n", func, line, p->rtc_diff);
308 pr_debug("%s:%d: p.av_multi_out %u\n", func, line, p->av_multi_out);
309 pr_debug("%s:%d: p.ctrl_button: %u\n", func, line, p->ctrl_button);
310 pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func, line,
311 p->static_ip_addr[0], p->static_ip_addr[1],
312 p->static_ip_addr[2], p->static_ip_addr[3]);
313 pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func, line,
314 p->network_mask[0], p->network_mask[1],
315 p->network_mask[2], p->network_mask[3]);
316 pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func, line,
317 p->default_gateway[0], p->default_gateway[1],
318 p->default_gateway[2], p->default_gateway[3]);
319 pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func, line,
320 p->dns_primary[0], p->dns_primary[1],
321 p->dns_primary[2], p->dns_primary[3]);
322 pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func, line,
323 p->dns_secondary[0], p->dns_secondary[1],
324 p->dns_secondary[2], p->dns_secondary[3]);
325}
326
Geoff Levandef2ac632007-10-09 11:07:24 +1000327static int verify_header(const struct os_area_header *header)
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100328{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100329 if (memcmp(header->magic_num, OS_AREA_HEADER_MAGIC_NUM,
330 sizeof(header->magic_num))) {
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100331 pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
332 return -1;
333 }
334
335 if (header->hdr_version < 1) {
336 pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
337 return -1;
338 }
339
Geoff Levandef2ac632007-10-09 11:07:24 +1000340 if (header->db_area_offset > header->ldr_area_offset) {
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100341 pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
342 return -1;
343 }
344
345 return 0;
346}
347
Geoff Levandef2ac632007-10-09 11:07:24 +1000348static int db_verify(const struct os_area_db *db)
349{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100350 if (memcmp(db->magic_num, OS_AREA_DB_MAGIC_NUM,
351 sizeof(db->magic_num))) {
Geoff Levandef2ac632007-10-09 11:07:24 +1000352 pr_debug("%s:%d magic_num failed\n", __func__, __LINE__);
353 return -1;
354 }
355
356 if (db->version != 1) {
357 pr_debug("%s:%d version failed\n", __func__, __LINE__);
358 return -1;
359 }
360
361 return 0;
362}
363
364struct db_index {
365 uint8_t owner:5;
366 uint8_t key:3;
367};
368
369struct db_iterator {
370 const struct os_area_db *db;
371 struct os_area_db_id match_id;
372 struct db_index *idx;
373 struct db_index *last_idx;
374 union {
375 uint64_t *value_64;
376 uint32_t *value_32;
377 uint16_t *value_16;
378 };
379};
380
381static unsigned int db_align_up(unsigned int val, unsigned int size)
382{
383 return (val + (size - 1)) & (~(size - 1));
384}
385
386/**
387 * db_for_each_64 - Iterator for 64 bit entries.
388 *
389 * A NULL value for id can be used to match all entries.
390 * OS_AREA_DB_OWNER_ANY and OS_AREA_DB_KEY_ANY can be used to match all.
391 */
392
393static int db_for_each_64(const struct os_area_db *db,
394 const struct os_area_db_id *match_id, struct db_iterator *i)
395{
396next:
397 if (!i->db) {
398 i->db = db;
399 i->match_id = match_id ? *match_id : os_area_db_id_any;
400 i->idx = (void *)db + db->index_64;
401 i->last_idx = i->idx + db->count_64;
402 i->value_64 = (void *)db + db->index_64
403 + db_align_up(db->count_64, 8);
404 } else {
405 i->idx++;
406 i->value_64++;
407 }
408
409 if (i->idx >= i->last_idx) {
410 pr_debug("%s:%d: reached end\n", __func__, __LINE__);
411 return 0;
412 }
413
414 if (i->match_id.owner != OS_AREA_DB_OWNER_ANY
415 && i->match_id.owner != (int)i->idx->owner)
416 goto next;
417 if (i->match_id.key != OS_AREA_DB_KEY_ANY
418 && i->match_id.key != (int)i->idx->key)
419 goto next;
420
421 return 1;
422}
423
424static int db_delete_64(struct os_area_db *db, const struct os_area_db_id *id)
425{
426 struct db_iterator i;
427
428 for (i.db = NULL; db_for_each_64(db, id, &i); ) {
429
430 pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__, __LINE__,
431 i.idx->owner, i.idx->key,
432 (unsigned long long)*i.value_64);
433
434 i.idx->owner = 0;
435 i.idx->key = 0;
436 *i.value_64 = 0;
437 }
438 return 0;
439}
440
441static int db_set_64(struct os_area_db *db, const struct os_area_db_id *id,
442 uint64_t value)
443{
444 struct db_iterator i;
445
446 pr_debug("%s:%d: (%d:%d) <= %llxh\n", __func__, __LINE__,
447 id->owner, id->key, (unsigned long long)value);
448
449 if (!id->owner || id->owner == OS_AREA_DB_OWNER_ANY
450 || id->key == OS_AREA_DB_KEY_ANY) {
451 pr_debug("%s:%d: bad id: (%d:%d)\n", __func__,
452 __LINE__, id->owner, id->key);
453 return -1;
454 }
455
456 db_delete_64(db, id);
457
458 i.db = NULL;
459 if (db_for_each_64(db, &os_area_db_id_empty, &i)) {
460
461 pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__, __LINE__,
462 i.idx->owner, i.idx->key,
463 (unsigned long long)*i.value_64);
464
465 i.idx->owner = id->owner;
466 i.idx->key = id->key;
467 *i.value_64 = value;
468
469 pr_debug("%s:%d: set (%d:%d) <= %llxh\n", __func__, __LINE__,
470 i.idx->owner, i.idx->key,
471 (unsigned long long)*i.value_64);
472 return 0;
473 }
474 pr_debug("%s:%d: database full.\n",
475 __func__, __LINE__);
476 return -1;
477}
478
479static int db_get_64(const struct os_area_db *db,
480 const struct os_area_db_id *id, uint64_t *value)
481{
482 struct db_iterator i;
483
484 i.db = NULL;
485 if (db_for_each_64(db, id, &i)) {
486 *value = *i.value_64;
487 pr_debug("%s:%d: found %lld\n", __func__, __LINE__,
488 (long long int)*i.value_64);
489 return 0;
490 }
491 pr_debug("%s:%d: not found\n", __func__, __LINE__);
492 return -1;
493}
494
495static int db_get_rtc_diff(const struct os_area_db *db, int64_t *rtc_diff)
496{
497 return db_get_64(db, &os_area_db_id_rtc_diff, (uint64_t*)rtc_diff);
498}
499
500#define dump_db(a) _dump_db(a, __func__, __LINE__)
501static void _dump_db(const struct os_area_db *db, const char *func,
502 int line)
503{
Geoff Levandec5d2df2007-12-24 04:41:01 +1100504 char str[sizeof(db->magic_num) + 1];
505
506 dump_field(str, db->magic_num, sizeof(db->magic_num));
Geoff Levandef2ac632007-10-09 11:07:24 +1000507 pr_debug("%s:%d: db.magic_num: '%s'\n", func, line,
Geoff Levandec5d2df2007-12-24 04:41:01 +1100508 str);
Geoff Levandef2ac632007-10-09 11:07:24 +1000509 pr_debug("%s:%d: db.version: %u\n", func, line,
510 db->version);
511 pr_debug("%s:%d: db.index_64: %u\n", func, line,
512 db->index_64);
513 pr_debug("%s:%d: db.count_64: %u\n", func, line,
514 db->count_64);
515 pr_debug("%s:%d: db.index_32: %u\n", func, line,
516 db->index_32);
517 pr_debug("%s:%d: db.count_32: %u\n", func, line,
518 db->count_32);
519 pr_debug("%s:%d: db.index_16: %u\n", func, line,
520 db->index_16);
521 pr_debug("%s:%d: db.count_16: %u\n", func, line,
522 db->count_16);
523}
524
525static void os_area_db_init(struct os_area_db *db)
526{
527 enum {
528 HEADER_SIZE = offsetof(struct os_area_db, _db_data),
529 INDEX_64_COUNT = 64,
530 VALUES_64_COUNT = 57,
531 INDEX_32_COUNT = 64,
532 VALUES_32_COUNT = 57,
533 INDEX_16_COUNT = 64,
534 VALUES_16_COUNT = 57,
535 };
536
537 memset(db, 0, sizeof(struct os_area_db));
538
Geoff Levandec5d2df2007-12-24 04:41:01 +1100539 memcpy(db->magic_num, OS_AREA_DB_MAGIC_NUM, sizeof(db->magic_num));
Geoff Levandef2ac632007-10-09 11:07:24 +1000540 db->version = 1;
541 db->index_64 = HEADER_SIZE;
542 db->count_64 = VALUES_64_COUNT;
543 db->index_32 = HEADER_SIZE
544 + INDEX_64_COUNT * sizeof(struct db_index)
545 + VALUES_64_COUNT * sizeof(u64);
546 db->count_32 = VALUES_32_COUNT;
547 db->index_16 = HEADER_SIZE
548 + INDEX_64_COUNT * sizeof(struct db_index)
549 + VALUES_64_COUNT * sizeof(u64)
550 + INDEX_32_COUNT * sizeof(struct db_index)
551 + VALUES_32_COUNT * sizeof(u32);
552 db->count_16 = VALUES_16_COUNT;
553
554 /* Rules to check db layout. */
555
556 BUILD_BUG_ON(sizeof(struct db_index) != 1);
557 BUILD_BUG_ON(sizeof(struct os_area_db) != 2 * OS_AREA_SEGMENT_SIZE);
558 BUILD_BUG_ON(INDEX_64_COUNT & 0x7);
559 BUILD_BUG_ON(VALUES_64_COUNT > INDEX_64_COUNT);
560 BUILD_BUG_ON(INDEX_32_COUNT & 0x7);
561 BUILD_BUG_ON(VALUES_32_COUNT > INDEX_32_COUNT);
562 BUILD_BUG_ON(INDEX_16_COUNT & 0x7);
563 BUILD_BUG_ON(VALUES_16_COUNT > INDEX_16_COUNT);
564 BUILD_BUG_ON(HEADER_SIZE
565 + INDEX_64_COUNT * sizeof(struct db_index)
566 + VALUES_64_COUNT * sizeof(u64)
567 + INDEX_32_COUNT * sizeof(struct db_index)
568 + VALUES_32_COUNT * sizeof(u32)
569 + INDEX_16_COUNT * sizeof(struct db_index)
570 + VALUES_16_COUNT * sizeof(u16)
571 > sizeof(struct os_area_db));
572}
573
574/**
575 * update_flash_db - Helper for os_area_queue_work_handler.
576 *
577 */
578
579static void update_flash_db(void)
580{
581 int result;
582 int file;
583 off_t offset;
584 ssize_t count;
585 static const unsigned int buf_len = 8 * OS_AREA_SEGMENT_SIZE;
586 const struct os_area_header *header;
587 struct os_area_db* db;
588
589 /* Read in header and db from flash. */
590
591 file = sys_open("/dev/ps3flash", O_RDWR, 0);
592
593 if (file < 0) {
594 pr_debug("%s:%d sys_open failed\n", __func__, __LINE__);
595 goto fail_open;
596 }
597
598 header = kmalloc(buf_len, GFP_KERNEL);
599
600 if (!header) {
601 pr_debug("%s:%d kmalloc failed\n", __func__, __LINE__);
602 goto fail_malloc;
603 }
604
605 offset = sys_lseek(file, 0, SEEK_SET);
606
607 if (offset != 0) {
608 pr_debug("%s:%d sys_lseek failed\n", __func__, __LINE__);
609 goto fail_header_seek;
610 }
611
612 count = sys_read(file, (char __user *)header, buf_len);
613
614 result = count < OS_AREA_SEGMENT_SIZE || verify_header(header)
615 || count < header->db_area_offset * OS_AREA_SEGMENT_SIZE;
616
617 if (result) {
618 pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
619 dump_header(header);
620 goto fail_header;
621 }
622
623 /* Now got a good db offset and some maybe good db data. */
624
625 db = (void*)header + header->db_area_offset * OS_AREA_SEGMENT_SIZE;
626
627 result = db_verify(db);
628
629 if (result) {
630 printk(KERN_NOTICE "%s:%d: Verify of flash database failed, "
631 "formatting.\n", __func__, __LINE__);
632 dump_db(db);
633 os_area_db_init(db);
634 }
635
636 /* Now got good db data. */
637
638 db_set_64(db, &os_area_db_id_rtc_diff, saved_params.rtc_diff);
639
640 offset = sys_lseek(file, header->db_area_offset * OS_AREA_SEGMENT_SIZE,
641 SEEK_SET);
642
643 if (offset != header->db_area_offset * OS_AREA_SEGMENT_SIZE) {
644 pr_debug("%s:%d sys_lseek failed\n", __func__, __LINE__);
645 goto fail_db_seek;
646 }
647
648 count = sys_write(file, (const char __user *)db,
649 sizeof(struct os_area_db));
650
651 if (count < sizeof(struct os_area_db)) {
652 pr_debug("%s:%d sys_write failed\n", __func__, __LINE__);
653 }
654
655fail_db_seek:
656fail_header:
657fail_header_seek:
658 kfree(header);
659fail_malloc:
660 sys_close(file);
661fail_open:
662 return;
663}
664
Geoff Levand01263e82007-10-07 07:35:44 +1000665/**
Geoff Levand418ef202007-10-07 07:35:45 +1000666 * os_area_queue_work_handler - Asynchronous write handler.
667 *
668 * An asynchronous write for flash memory and the device tree. Do not
669 * call directly, use os_area_queue_work().
670 */
671
672static void os_area_queue_work_handler(struct work_struct *work)
673{
Geoff Levand7db19422007-10-07 07:35:47 +1000674 struct device_node *node;
675
Geoff Levand418ef202007-10-07 07:35:45 +1000676 pr_debug(" -> %s:%d\n", __func__, __LINE__);
677
Geoff Levand7db19422007-10-07 07:35:47 +1000678 node = of_find_node_by_path("/");
679
680 if (node) {
681 os_area_set_property(node, &property_rtc_diff);
682 of_node_put(node);
683 } else
684 pr_debug("%s:%d of_find_node_by_path failed\n",
685 __func__, __LINE__);
686
Geoff Levandef2ac632007-10-09 11:07:24 +1000687#if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
688 update_flash_db();
689#else
690 printk(KERN_WARNING "%s:%d: No flash rom driver configured.\n",
691 __func__, __LINE__);
692#endif
Geoff Levand418ef202007-10-07 07:35:45 +1000693 pr_debug(" <- %s:%d\n", __func__, __LINE__);
694}
695
696static void os_area_queue_work(void)
697{
698 static DECLARE_WORK(q, os_area_queue_work_handler);
699
700 wmb();
701 schedule_work(&q);
702}
703
704/**
Geoff Levand01263e82007-10-07 07:35:44 +1000705 * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
706 *
Geoff Levandef2ac632007-10-09 11:07:24 +1000707 * For the convenience of the guest the HV makes a copy of the os area in
Geoff Levand01263e82007-10-07 07:35:44 +1000708 * flash to a high address in the boot memory region and then puts that RAM
Geoff Levandef2ac632007-10-09 11:07:24 +1000709 * address and the byte count into the repository for retrieval by the guest.
Geoff Levand01263e82007-10-07 07:35:44 +1000710 * We copy the data we want into a static variable and allow the memory setup
711 * by the HV to be claimed by the lmb manager.
Geoff Levandef2ac632007-10-09 11:07:24 +1000712 *
713 * The os area mirror will not be available to a second stage kernel, and
714 * the header verify will fail. In this case, the saved_params values will
715 * be set from flash memory or the passed in device tree in ps3_os_area_init().
Geoff Levand01263e82007-10-07 07:35:44 +1000716 */
717
718void __init ps3_os_area_save_params(void)
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100719{
720 int result;
721 u64 lpar_addr;
722 unsigned int size;
723 struct os_area_header *header;
724 struct os_area_params *params;
Geoff Levandef2ac632007-10-09 11:07:24 +1000725 struct os_area_db *db;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100726
Geoff Levand01263e82007-10-07 07:35:44 +1000727 pr_debug(" -> %s:%d\n", __func__, __LINE__);
728
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100729 result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
730
731 if (result) {
732 pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
733 __func__, __LINE__);
Geoff Levand01263e82007-10-07 07:35:44 +1000734 return;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100735 }
736
737 header = (struct os_area_header *)__va(lpar_addr);
Geoff Levandca942972007-10-07 07:35:43 +1000738 params = (struct os_area_params *)__va(lpar_addr
739 + OS_AREA_SEGMENT_SIZE);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100740
741 result = verify_header(header);
742
743 if (result) {
Geoff Levand7db19422007-10-07 07:35:47 +1000744 /* Second stage kernels exit here. */
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100745 pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
746 dump_header(header);
Geoff Levand01263e82007-10-07 07:35:44 +1000747 return;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100748 }
749
Geoff Levandef2ac632007-10-09 11:07:24 +1000750 db = (struct os_area_db *)__va(lpar_addr
751 + header->db_area_offset * OS_AREA_SEGMENT_SIZE);
752
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100753 dump_header(header);
754 dump_params(params);
Geoff Levandef2ac632007-10-09 11:07:24 +1000755 dump_db(db);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100756
Geoff Levandef2ac632007-10-09 11:07:24 +1000757 result = db_verify(db) || db_get_rtc_diff(db, &saved_params.rtc_diff);
758 if (result)
759 saved_params.rtc_diff = params->rtc_diff ? params->rtc_diff
760 : SECONDS_FROM_1970_TO_2000;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100761 saved_params.av_multi_out = params->av_multi_out;
Geoff Levand7db19422007-10-07 07:35:47 +1000762 saved_params.valid = 1;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100763
Geoff Levand01263e82007-10-07 07:35:44 +1000764 memset(header, 0, sizeof(*header));
765
766 pr_debug(" <- %s:%d\n", __func__, __LINE__);
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100767}
768
769/**
Geoff Levand7db19422007-10-07 07:35:47 +1000770 * ps3_os_area_init - Setup os area device tree properties as needed.
771 */
772
773void __init ps3_os_area_init(void)
774{
775 struct device_node *node;
776
777 pr_debug(" -> %s:%d\n", __func__, __LINE__);
778
779 node = of_find_node_by_path("/");
780
781 if (!saved_params.valid && node) {
782 /* Second stage kernels should have a dt entry. */
783 os_area_get_property(node, &property_rtc_diff);
784 os_area_get_property(node, &property_av_multi_out);
785 }
786
787 if(!saved_params.rtc_diff)
788 saved_params.rtc_diff = SECONDS_FROM_1970_TO_2000;
789
790 if (node) {
791 os_area_set_property(node, &property_rtc_diff);
792 os_area_set_property(node, &property_av_multi_out);
793 of_node_put(node);
794 } else
795 pr_debug("%s:%d of_find_node_by_path failed\n",
796 __func__, __LINE__);
797
798 pr_debug(" <- %s:%d\n", __func__, __LINE__);
799}
800
801/**
Geoff Levandd7b98e32007-10-07 07:35:46 +1000802 * ps3_os_area_get_rtc_diff - Returns the rtc diff value.
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100803 */
804
Geoff Levandd7b98e32007-10-07 07:35:46 +1000805u64 ps3_os_area_get_rtc_diff(void)
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100806{
Geoff Levand7db19422007-10-07 07:35:47 +1000807 return saved_params.rtc_diff;
Geoff Levand00a3e2e2006-11-23 00:46:58 +0100808}
Geert Uytterhoeven098e2742007-01-26 19:08:24 -0800809
810/**
Geoff Levandd7b98e32007-10-07 07:35:46 +1000811 * ps3_os_area_set_rtc_diff - Set the rtc diff value.
812 *
813 * An asynchronous write is needed to support writing updates from
814 * the timer interrupt context.
815 */
816
817void ps3_os_area_set_rtc_diff(u64 rtc_diff)
818{
819 if (saved_params.rtc_diff != rtc_diff) {
820 saved_params.rtc_diff = rtc_diff;
821 os_area_queue_work();
822 }
823}
824
825/**
Geert Uytterhoeven098e2742007-01-26 19:08:24 -0800826 * ps3_os_area_get_av_multi_out - Returns the default video mode.
827 */
828
829enum ps3_param_av_multi_out ps3_os_area_get_av_multi_out(void)
830{
831 return saved_params.av_multi_out;
832}
833EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out);