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