2 * PS3 flash memory os area.
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
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.
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.
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
21 #include <linux/kernel.h>
23 #include <linux/workqueue.h>
25 #include <linux/syscalls.h>
32 OS_AREA_SEGMENT_SIZE
= 0X200,
35 enum os_area_ldr_format
{
36 HEADER_LDR_FORMAT_RAW
= 0,
37 HEADER_LDR_FORMAT_GZIP
= 1,
41 * struct os_area_header - os area header segment.
42 * @magic_num: Always 'cell_ext_os_area'.
43 * @hdr_version: Header format version number.
44 * @db_area_offset: Starting segment number of other os database area.
45 * @ldr_area_offset: Starting segment number of bootloader image area.
46 * @ldr_format: HEADER_LDR_FORMAT flag.
47 * @ldr_size: Size of bootloader image in bytes.
49 * Note that the docs refer to area offsets. These are offsets in units of
50 * segments from the start of the os area (top of the header). These are
51 * better thought of as segment numbers. The os area of the os area is
52 * reserved for the os image.
55 struct os_area_header
{
66 enum os_area_boot_flag
{
67 PARAM_BOOT_FLAG_GAME_OS
= 0,
68 PARAM_BOOT_FLAG_OTHER_OS
= 1,
71 enum os_area_ctrl_button
{
72 PARAM_CTRL_BUTTON_O_IS_YES
= 0,
73 PARAM_CTRL_BUTTON_X_IS_YES
= 1,
77 * struct os_area_params - os area params segment.
78 * @boot_flag: User preference of operating system, PARAM_BOOT_FLAG flag.
79 * @num_params: Number of params in this (params) segment.
80 * @rtc_diff: Difference in seconds between 1970 and the ps3 rtc value.
81 * @av_multi_out: User preference of AV output, PARAM_AV_MULTI_OUT flag.
82 * @ctrl_button: User preference of controller button config, PARAM_CTRL_BUTTON
84 * @static_ip_addr: User preference of static IP address.
85 * @network_mask: User preference of static network mask.
86 * @default_gateway: User preference of static default gateway.
87 * @dns_primary: User preference of static primary dns server.
88 * @dns_secondary: User preference of static secondary dns server.
90 * The ps3 rtc maintains a read-only value that approximates seconds since
91 * 2000-01-01 00:00:00 UTC.
93 * User preference of zero for static_ip_addr means use dhcp.
96 struct os_area_params
{
107 u8 static_ip_addr
[4];
109 u8 default_gateway
[4];
118 OS_AREA_DB_MAGIC_NUM
= 0x2d64622dU
,
122 * struct os_area_db - Shared flash memory database.
123 * @magic_num: Always '-db-' = 0x2d64622d.
124 * @version: os_area_db format version number.
125 * @index_64: byte offset of the database id index for 64 bit variables.
126 * @count_64: number of usable 64 bit index entries
127 * @index_32: byte offset of the database id index for 32 bit variables.
128 * @count_32: number of usable 32 bit index entries
129 * @index_16: byte offset of the database id index for 16 bit variables.
130 * @count_16: number of usable 16 bit index entries
132 * Flash rom storage for exclusive use by guests running in the other os lpar.
133 * The current system configuration allocates 1K (two segments) for other os
152 * enum os_area_db_owner - Data owners.
155 enum os_area_db_owner
{
156 OS_AREA_DB_OWNER_ANY
= -1,
157 OS_AREA_DB_OWNER_NONE
= 0,
158 OS_AREA_DB_OWNER_PROTOTYPE
= 1,
159 OS_AREA_DB_OWNER_LINUX
= 2,
160 OS_AREA_DB_OWNER_PETITBOOT
= 3,
161 OS_AREA_DB_OWNER_MAX
= 32,
164 enum os_area_db_key
{
165 OS_AREA_DB_KEY_ANY
= -1,
166 OS_AREA_DB_KEY_NONE
= 0,
167 OS_AREA_DB_KEY_RTC_DIFF
= 1,
168 OS_AREA_DB_KEY_VIDEO_MODE
= 2,
169 OS_AREA_DB_KEY_MAX
= 8,
172 struct os_area_db_id
{
177 static const struct os_area_db_id os_area_db_id_empty
= {
178 .owner
= OS_AREA_DB_OWNER_NONE
,
179 .key
= OS_AREA_DB_KEY_NONE
182 static const struct os_area_db_id os_area_db_id_any
= {
183 .owner
= OS_AREA_DB_OWNER_ANY
,
184 .key
= OS_AREA_DB_KEY_ANY
187 static const struct os_area_db_id os_area_db_id_rtc_diff
= {
188 .owner
= OS_AREA_DB_OWNER_LINUX
,
189 .key
= OS_AREA_DB_KEY_RTC_DIFF
192 static const struct os_area_db_id os_area_db_id_video_mode
= {
193 .owner
= OS_AREA_DB_OWNER_LINUX
,
194 .key
= OS_AREA_DB_KEY_VIDEO_MODE
197 #define SECONDS_FROM_1970_TO_2000 946684800LL
200 * struct saved_params - Static working copies of data from the PS3 'os area'.
202 * The order of preference we use for the rtc_diff source:
203 * 1) The database value.
204 * 2) The game os value.
205 * 3) The number of seconds from 1970 to 2000.
208 struct saved_params
{
211 unsigned int av_multi_out
;
212 } static saved_params
;
214 static struct property property_rtc_diff
= {
215 .name
= "linux,rtc_diff",
216 .length
= sizeof(saved_params
.rtc_diff
),
217 .value
= &saved_params
.rtc_diff
,
220 static struct property property_av_multi_out
= {
221 .name
= "linux,av_multi_out",
222 .length
= sizeof(saved_params
.av_multi_out
),
223 .value
= &saved_params
.av_multi_out
,
227 * os_area_set_property - Add or overwrite a saved_params value to the device tree.
229 * Overwrites an existing property.
232 static void os_area_set_property(struct device_node
*node
,
233 struct property
*prop
)
236 struct property
*tmp
= of_find_property(node
, prop
->name
, NULL
);
239 pr_debug("%s:%d found %s\n", __func__
, __LINE__
, prop
->name
);
240 prom_remove_property(node
, tmp
);
243 result
= prom_add_property(node
, prop
);
246 pr_debug("%s:%d prom_set_property failed\n", __func__
,
251 * os_area_get_property - Get a saved_params value from the device tree.
255 static void __init
os_area_get_property(struct device_node
*node
,
256 struct property
*prop
)
258 const struct property
*tmp
= of_find_property(node
, prop
->name
, NULL
);
261 BUG_ON(prop
->length
!= tmp
->length
);
262 memcpy(prop
->value
, tmp
->value
, prop
->length
);
264 pr_debug("%s:%d not found %s\n", __func__
, __LINE__
,
268 #define dump_header(_a) _dump_header(_a, __func__, __LINE__)
269 static void _dump_header(const struct os_area_header
*h
, const char *func
,
272 pr_debug("%s:%d: h.magic_num: '%s'\n", func
, line
,
274 pr_debug("%s:%d: h.hdr_version: %u\n", func
, line
,
276 pr_debug("%s:%d: h.db_area_offset: %u\n", func
, line
,
278 pr_debug("%s:%d: h.ldr_area_offset: %u\n", func
, line
,
280 pr_debug("%s:%d: h.ldr_format: %u\n", func
, line
,
282 pr_debug("%s:%d: h.ldr_size: %xh\n", func
, line
,
286 #define dump_params(_a) _dump_params(_a, __func__, __LINE__)
287 static void _dump_params(const struct os_area_params
*p
, const char *func
,
290 pr_debug("%s:%d: p.boot_flag: %u\n", func
, line
, p
->boot_flag
);
291 pr_debug("%s:%d: p.num_params: %u\n", func
, line
, p
->num_params
);
292 pr_debug("%s:%d: p.rtc_diff %ld\n", func
, line
, p
->rtc_diff
);
293 pr_debug("%s:%d: p.av_multi_out %u\n", func
, line
, p
->av_multi_out
);
294 pr_debug("%s:%d: p.ctrl_button: %u\n", func
, line
, p
->ctrl_button
);
295 pr_debug("%s:%d: p.static_ip_addr: %u.%u.%u.%u\n", func
, line
,
296 p
->static_ip_addr
[0], p
->static_ip_addr
[1],
297 p
->static_ip_addr
[2], p
->static_ip_addr
[3]);
298 pr_debug("%s:%d: p.network_mask: %u.%u.%u.%u\n", func
, line
,
299 p
->network_mask
[0], p
->network_mask
[1],
300 p
->network_mask
[2], p
->network_mask
[3]);
301 pr_debug("%s:%d: p.default_gateway: %u.%u.%u.%u\n", func
, line
,
302 p
->default_gateway
[0], p
->default_gateway
[1],
303 p
->default_gateway
[2], p
->default_gateway
[3]);
304 pr_debug("%s:%d: p.dns_primary: %u.%u.%u.%u\n", func
, line
,
305 p
->dns_primary
[0], p
->dns_primary
[1],
306 p
->dns_primary
[2], p
->dns_primary
[3]);
307 pr_debug("%s:%d: p.dns_secondary: %u.%u.%u.%u\n", func
, line
,
308 p
->dns_secondary
[0], p
->dns_secondary
[1],
309 p
->dns_secondary
[2], p
->dns_secondary
[3]);
312 static int verify_header(const struct os_area_header
*header
)
314 if (memcmp(header
->magic_num
, "cell_ext_os_area", 16)) {
315 pr_debug("%s:%d magic_num failed\n", __func__
, __LINE__
);
319 if (header
->hdr_version
< 1) {
320 pr_debug("%s:%d hdr_version failed\n", __func__
, __LINE__
);
324 if (header
->db_area_offset
> header
->ldr_area_offset
) {
325 pr_debug("%s:%d offsets failed\n", __func__
, __LINE__
);
332 static int db_verify(const struct os_area_db
*db
)
334 if (db
->magic_num
!= OS_AREA_DB_MAGIC_NUM
) {
335 pr_debug("%s:%d magic_num failed\n", __func__
, __LINE__
);
339 if (db
->version
!= 1) {
340 pr_debug("%s:%d version failed\n", __func__
, __LINE__
);
353 const struct os_area_db
*db
;
354 struct os_area_db_id match_id
;
355 struct db_index
*idx
;
356 struct db_index
*last_idx
;
364 static unsigned int db_align_up(unsigned int val
, unsigned int size
)
366 return (val
+ (size
- 1)) & (~(size
- 1));
370 * db_for_each_64 - Iterator for 64 bit entries.
372 * A NULL value for id can be used to match all entries.
373 * OS_AREA_DB_OWNER_ANY and OS_AREA_DB_KEY_ANY can be used to match all.
376 static int db_for_each_64(const struct os_area_db
*db
,
377 const struct os_area_db_id
*match_id
, struct db_iterator
*i
)
382 i
->match_id
= match_id
? *match_id
: os_area_db_id_any
;
383 i
->idx
= (void *)db
+ db
->index_64
;
384 i
->last_idx
= i
->idx
+ db
->count_64
;
385 i
->value_64
= (void *)db
+ db
->index_64
386 + db_align_up(db
->count_64
, 8);
392 if (i
->idx
>= i
->last_idx
) {
393 pr_debug("%s:%d: reached end\n", __func__
, __LINE__
);
397 if (i
->match_id
.owner
!= OS_AREA_DB_OWNER_ANY
398 && i
->match_id
.owner
!= (int)i
->idx
->owner
)
400 if (i
->match_id
.key
!= OS_AREA_DB_KEY_ANY
401 && i
->match_id
.key
!= (int)i
->idx
->key
)
407 static int db_delete_64(struct os_area_db
*db
, const struct os_area_db_id
*id
)
409 struct db_iterator i
;
411 for (i
.db
= NULL
; db_for_each_64(db
, id
, &i
); ) {
413 pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__
, __LINE__
,
414 i
.idx
->owner
, i
.idx
->key
,
415 (unsigned long long)*i
.value_64
);
424 static int db_set_64(struct os_area_db
*db
, const struct os_area_db_id
*id
,
427 struct db_iterator i
;
429 pr_debug("%s:%d: (%d:%d) <= %llxh\n", __func__
, __LINE__
,
430 id
->owner
, id
->key
, (unsigned long long)value
);
432 if (!id
->owner
|| id
->owner
== OS_AREA_DB_OWNER_ANY
433 || id
->key
== OS_AREA_DB_KEY_ANY
) {
434 pr_debug("%s:%d: bad id: (%d:%d)\n", __func__
,
435 __LINE__
, id
->owner
, id
->key
);
439 db_delete_64(db
, id
);
442 if (db_for_each_64(db
, &os_area_db_id_empty
, &i
)) {
444 pr_debug("%s:%d: got (%d:%d) %llxh\n", __func__
, __LINE__
,
445 i
.idx
->owner
, i
.idx
->key
,
446 (unsigned long long)*i
.value_64
);
448 i
.idx
->owner
= id
->owner
;
449 i
.idx
->key
= id
->key
;
452 pr_debug("%s:%d: set (%d:%d) <= %llxh\n", __func__
, __LINE__
,
453 i
.idx
->owner
, i
.idx
->key
,
454 (unsigned long long)*i
.value_64
);
457 pr_debug("%s:%d: database full.\n",
462 static int db_get_64(const struct os_area_db
*db
,
463 const struct os_area_db_id
*id
, uint64_t *value
)
465 struct db_iterator i
;
468 if (db_for_each_64(db
, id
, &i
)) {
469 *value
= *i
.value_64
;
470 pr_debug("%s:%d: found %lld\n", __func__
, __LINE__
,
471 (long long int)*i
.value_64
);
474 pr_debug("%s:%d: not found\n", __func__
, __LINE__
);
478 static int db_get_rtc_diff(const struct os_area_db
*db
, int64_t *rtc_diff
)
480 return db_get_64(db
, &os_area_db_id_rtc_diff
, (uint64_t*)rtc_diff
);
483 #define dump_db(a) _dump_db(a, __func__, __LINE__)
484 static void _dump_db(const struct os_area_db
*db
, const char *func
,
487 pr_debug("%s:%d: db.magic_num: '%s'\n", func
, line
,
488 (const char*)&db
->magic_num
);
489 pr_debug("%s:%d: db.version: %u\n", func
, line
,
491 pr_debug("%s:%d: db.index_64: %u\n", func
, line
,
493 pr_debug("%s:%d: db.count_64: %u\n", func
, line
,
495 pr_debug("%s:%d: db.index_32: %u\n", func
, line
,
497 pr_debug("%s:%d: db.count_32: %u\n", func
, line
,
499 pr_debug("%s:%d: db.index_16: %u\n", func
, line
,
501 pr_debug("%s:%d: db.count_16: %u\n", func
, line
,
505 static void os_area_db_init(struct os_area_db
*db
)
508 HEADER_SIZE
= offsetof(struct os_area_db
, _db_data
),
510 VALUES_64_COUNT
= 57,
512 VALUES_32_COUNT
= 57,
514 VALUES_16_COUNT
= 57,
517 memset(db
, 0, sizeof(struct os_area_db
));
519 db
->magic_num
= OS_AREA_DB_MAGIC_NUM
;
521 db
->index_64
= HEADER_SIZE
;
522 db
->count_64
= VALUES_64_COUNT
;
523 db
->index_32
= HEADER_SIZE
524 + INDEX_64_COUNT
* sizeof(struct db_index
)
525 + VALUES_64_COUNT
* sizeof(u64
);
526 db
->count_32
= VALUES_32_COUNT
;
527 db
->index_16
= HEADER_SIZE
528 + INDEX_64_COUNT
* sizeof(struct db_index
)
529 + VALUES_64_COUNT
* sizeof(u64
)
530 + INDEX_32_COUNT
* sizeof(struct db_index
)
531 + VALUES_32_COUNT
* sizeof(u32
);
532 db
->count_16
= VALUES_16_COUNT
;
534 /* Rules to check db layout. */
536 BUILD_BUG_ON(sizeof(struct db_index
) != 1);
537 BUILD_BUG_ON(sizeof(struct os_area_db
) != 2 * OS_AREA_SEGMENT_SIZE
);
538 BUILD_BUG_ON(INDEX_64_COUNT
& 0x7);
539 BUILD_BUG_ON(VALUES_64_COUNT
> INDEX_64_COUNT
);
540 BUILD_BUG_ON(INDEX_32_COUNT
& 0x7);
541 BUILD_BUG_ON(VALUES_32_COUNT
> INDEX_32_COUNT
);
542 BUILD_BUG_ON(INDEX_16_COUNT
& 0x7);
543 BUILD_BUG_ON(VALUES_16_COUNT
> INDEX_16_COUNT
);
544 BUILD_BUG_ON(HEADER_SIZE
545 + INDEX_64_COUNT
* sizeof(struct db_index
)
546 + VALUES_64_COUNT
* sizeof(u64
)
547 + INDEX_32_COUNT
* sizeof(struct db_index
)
548 + VALUES_32_COUNT
* sizeof(u32
)
549 + INDEX_16_COUNT
* sizeof(struct db_index
)
550 + VALUES_16_COUNT
* sizeof(u16
)
551 > sizeof(struct os_area_db
));
555 * update_flash_db - Helper for os_area_queue_work_handler.
559 static void update_flash_db(void)
565 static const unsigned int buf_len
= 8 * OS_AREA_SEGMENT_SIZE
;
566 const struct os_area_header
*header
;
567 struct os_area_db
* db
;
569 /* Read in header and db from flash. */
571 file
= sys_open("/dev/ps3flash", O_RDWR
, 0);
574 pr_debug("%s:%d sys_open failed\n", __func__
, __LINE__
);
578 header
= kmalloc(buf_len
, GFP_KERNEL
);
581 pr_debug("%s:%d kmalloc failed\n", __func__
, __LINE__
);
585 offset
= sys_lseek(file
, 0, SEEK_SET
);
588 pr_debug("%s:%d sys_lseek failed\n", __func__
, __LINE__
);
589 goto fail_header_seek
;
592 count
= sys_read(file
, (char __user
*)header
, buf_len
);
594 result
= count
< OS_AREA_SEGMENT_SIZE
|| verify_header(header
)
595 || count
< header
->db_area_offset
* OS_AREA_SEGMENT_SIZE
;
598 pr_debug("%s:%d verify_header failed\n", __func__
, __LINE__
);
603 /* Now got a good db offset and some maybe good db data. */
605 db
= (void*)header
+ header
->db_area_offset
* OS_AREA_SEGMENT_SIZE
;
607 result
= db_verify(db
);
610 printk(KERN_NOTICE
"%s:%d: Verify of flash database failed, "
611 "formatting.\n", __func__
, __LINE__
);
616 /* Now got good db data. */
618 db_set_64(db
, &os_area_db_id_rtc_diff
, saved_params
.rtc_diff
);
620 offset
= sys_lseek(file
, header
->db_area_offset
* OS_AREA_SEGMENT_SIZE
,
623 if (offset
!= header
->db_area_offset
* OS_AREA_SEGMENT_SIZE
) {
624 pr_debug("%s:%d sys_lseek failed\n", __func__
, __LINE__
);
628 count
= sys_write(file
, (const char __user
*)db
,
629 sizeof(struct os_area_db
));
631 if (count
< sizeof(struct os_area_db
)) {
632 pr_debug("%s:%d sys_write failed\n", __func__
, __LINE__
);
646 * os_area_queue_work_handler - Asynchronous write handler.
648 * An asynchronous write for flash memory and the device tree. Do not
649 * call directly, use os_area_queue_work().
652 static void os_area_queue_work_handler(struct work_struct
*work
)
654 struct device_node
*node
;
656 pr_debug(" -> %s:%d\n", __func__
, __LINE__
);
658 node
= of_find_node_by_path("/");
661 os_area_set_property(node
, &property_rtc_diff
);
664 pr_debug("%s:%d of_find_node_by_path failed\n",
667 #if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
670 printk(KERN_WARNING
"%s:%d: No flash rom driver configured.\n",
673 pr_debug(" <- %s:%d\n", __func__
, __LINE__
);
676 static void os_area_queue_work(void)
678 static DECLARE_WORK(q
, os_area_queue_work_handler
);
685 * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
687 * For the convenience of the guest the HV makes a copy of the os area in
688 * flash to a high address in the boot memory region and then puts that RAM
689 * address and the byte count into the repository for retrieval by the guest.
690 * We copy the data we want into a static variable and allow the memory setup
691 * by the HV to be claimed by the lmb manager.
693 * The os area mirror will not be available to a second stage kernel, and
694 * the header verify will fail. In this case, the saved_params values will
695 * be set from flash memory or the passed in device tree in ps3_os_area_init().
698 void __init
ps3_os_area_save_params(void)
703 struct os_area_header
*header
;
704 struct os_area_params
*params
;
705 struct os_area_db
*db
;
707 pr_debug(" -> %s:%d\n", __func__
, __LINE__
);
709 result
= ps3_repository_read_boot_dat_info(&lpar_addr
, &size
);
712 pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
717 header
= (struct os_area_header
*)__va(lpar_addr
);
718 params
= (struct os_area_params
*)__va(lpar_addr
719 + OS_AREA_SEGMENT_SIZE
);
721 result
= verify_header(header
);
724 /* Second stage kernels exit here. */
725 pr_debug("%s:%d verify_header failed\n", __func__
, __LINE__
);
730 db
= (struct os_area_db
*)__va(lpar_addr
731 + header
->db_area_offset
* OS_AREA_SEGMENT_SIZE
);
737 result
= db_verify(db
) || db_get_rtc_diff(db
, &saved_params
.rtc_diff
);
739 saved_params
.rtc_diff
= params
->rtc_diff
? params
->rtc_diff
740 : SECONDS_FROM_1970_TO_2000
;
741 saved_params
.av_multi_out
= params
->av_multi_out
;
742 saved_params
.valid
= 1;
744 memset(header
, 0, sizeof(*header
));
746 pr_debug(" <- %s:%d\n", __func__
, __LINE__
);
750 * ps3_os_area_init - Setup os area device tree properties as needed.
753 void __init
ps3_os_area_init(void)
755 struct device_node
*node
;
757 pr_debug(" -> %s:%d\n", __func__
, __LINE__
);
759 node
= of_find_node_by_path("/");
761 if (!saved_params
.valid
&& node
) {
762 /* Second stage kernels should have a dt entry. */
763 os_area_get_property(node
, &property_rtc_diff
);
764 os_area_get_property(node
, &property_av_multi_out
);
767 if(!saved_params
.rtc_diff
)
768 saved_params
.rtc_diff
= SECONDS_FROM_1970_TO_2000
;
771 os_area_set_property(node
, &property_rtc_diff
);
772 os_area_set_property(node
, &property_av_multi_out
);
775 pr_debug("%s:%d of_find_node_by_path failed\n",
778 pr_debug(" <- %s:%d\n", __func__
, __LINE__
);
782 * ps3_os_area_get_rtc_diff - Returns the rtc diff value.
785 u64
ps3_os_area_get_rtc_diff(void)
787 return saved_params
.rtc_diff
;
791 * ps3_os_area_set_rtc_diff - Set the rtc diff value.
793 * An asynchronous write is needed to support writing updates from
794 * the timer interrupt context.
797 void ps3_os_area_set_rtc_diff(u64 rtc_diff
)
799 if (saved_params
.rtc_diff
!= rtc_diff
) {
800 saved_params
.rtc_diff
= rtc_diff
;
801 os_area_queue_work();
806 * ps3_os_area_get_av_multi_out - Returns the default video mode.
809 enum ps3_param_av_multi_out
ps3_os_area_get_av_multi_out(void)
811 return saved_params
.av_multi_out
;
813 EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out
);