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>
26 #include <linux/ctype.h>
27 #include <linux/lmb.h>
35 OS_AREA_SEGMENT_SIZE = 0X200,
38 enum os_area_ldr_format {
39 HEADER_LDR_FORMAT_RAW = 0,
40 HEADER_LDR_FORMAT_GZIP = 1,
43 #define OS_AREA_HEADER_MAGIC_NUM "cell_ext_os_area"
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.
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.
60 struct os_area_header {
71 enum os_area_boot_flag {
72 PARAM_BOOT_FLAG_GAME_OS = 0,
73 PARAM_BOOT_FLAG_OTHER_OS = 1,
76 enum os_area_ctrl_button {
77 PARAM_CTRL_BUTTON_O_IS_YES = 0,
78 PARAM_CTRL_BUTTON_X_IS_YES = 1,
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
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.
95 * The ps3 rtc maintains a read-only value that approximates seconds since
96 * 2000-01-01 00:00:00 UTC.
98 * User preference of zero for static_ip_addr means use dhcp.
101 struct os_area_params {
112 u8 static_ip_addr[4];
114 u8 default_gateway[4];
122 #define OS_AREA_DB_MAGIC_NUM "-db-"
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
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
155 * enum os_area_db_owner - Data owners.
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,
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,
175 struct os_area_db_id {
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
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
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
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
200 #define SECONDS_FROM_1970_TO_2000 946684800LL
203 * struct saved_params - Static working copies of data from the PS3 'os area'.
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.
211 struct saved_params {
214 unsigned int av_multi_out;
215 } static saved_params;
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,
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,
230 * os_area_set_property - Add or overwrite a saved_params value to the device tree.
232 * Overwrites an existing property.
235 static void os_area_set_property(struct device_node *node,
236 struct property *prop)
239 struct property *tmp = of_find_property(node, prop->name, NULL);
242 pr_debug("%s:%d found %s\n", __func__, __LINE__, prop->name);
243 prom_remove_property(node, tmp);
246 result = prom_add_property(node, prop);
249 pr_debug("%s:%d prom_set_property failed\n", __func__,
254 * os_area_get_property - Get a saved_params value from the device tree.
258 static void __init os_area_get_property(struct device_node *node,
259 struct property *prop)
261 const struct property *tmp = of_find_property(node, prop->name, NULL);
264 BUG_ON(prop->length != tmp->length);
265 memcpy(prop->value, tmp->value, prop->length);
267 pr_debug("%s:%d not found %s\n", __func__, __LINE__,
271 static void dump_field(char *s, const u8 *field, int size_of_field)
276 for (i = 0; i < size_of_field; i++)
277 s[i] = isprint(field[i]) ? field[i] : '.';
282 #define dump_header(_a) _dump_header(_a, __func__, __LINE__)
283 static void _dump_header(const struct os_area_header *h, const char *func,
286 char str[sizeof(h->magic_num) + 1];
288 dump_field(str, h->magic_num, sizeof(h->magic_num));
289 pr_debug("%s:%d: h.magic_num: '%s'\n", func, line,
291 pr_debug("%s:%d: h.hdr_version: %u\n", func, line,
293 pr_debug("%s:%d: h.db_area_offset: %u\n", func, line,
295 pr_debug("%s:%d: h.ldr_area_offset: %u\n", func, line,
297 pr_debug("%s:%d: h.ldr_format: %u\n", func, line,
299 pr_debug("%s:%d: h.ldr_size: %xh\n", func, line,
303 #define dump_params(_a) _dump_params(_a, __func__, __LINE__)
304 static void _dump_params(const struct os_area_params *p, const char *func,
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]);
329 static int verify_header(const struct os_area_header *header)
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__);
337 if (header->hdr_version < 1) {
338 pr_debug("%s:%d hdr_version failed\n", __func__, __LINE__);
342 if (header->db_area_offset > header->ldr_area_offset) {
343 pr_debug("%s:%d offsets failed\n", __func__, __LINE__);
350 static int db_verify(const struct os_area_db *db)
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__);
358 if (db->version != 1) {
359 pr_debug("%s:%d version failed\n", __func__, __LINE__);
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;
383 static unsigned int db_align_up(unsigned int val, unsigned int size)
385 return (val + (size - 1)) & (~(size - 1));
389 * db_for_each_64 - Iterator for 64 bit entries.
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.
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)
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);
411 if (i->idx >= i->last_idx) {
412 pr_debug("%s:%d: reached end\n", __func__, __LINE__);
416 if (i->match_id.owner != OS_AREA_DB_OWNER_ANY
417 && i->match_id.owner != (int)i->idx->owner)
419 if (i->match_id.key != OS_AREA_DB_KEY_ANY
420 && i->match_id.key != (int)i->idx->key)
426 static int db_delete_64(struct os_area_db *db, const struct os_area_db_id *id)
428 struct db_iterator i;
430 for (i.db = NULL; db_for_each_64(db, id, &i); ) {
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);
443 static int db_set_64(struct os_area_db *db, const struct os_area_db_id *id,
446 struct db_iterator i;
448 pr_debug("%s:%d: (%d:%d) <= %llxh\n", __func__, __LINE__,
449 id->owner, id->key, (unsigned long long)value);
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);
458 db_delete_64(db, id);
461 if (db_for_each_64(db, &os_area_db_id_empty, &i)) {
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);
467 i.idx->owner = id->owner;
468 i.idx->key = id->key;
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);
476 pr_debug("%s:%d: database full.\n",
481 static int db_get_64(const struct os_area_db *db,
482 const struct os_area_db_id *id, uint64_t *value)
484 struct db_iterator i;
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);
493 pr_debug("%s:%d: not found\n", __func__, __LINE__);
497 static int db_get_rtc_diff(const struct os_area_db *db, int64_t *rtc_diff)
499 return db_get_64(db, &os_area_db_id_rtc_diff, (uint64_t*)rtc_diff);
502 #define dump_db(a) _dump_db(a, __func__, __LINE__)
503 static void _dump_db(const struct os_area_db *db, const char *func,
506 char str[sizeof(db->magic_num) + 1];
508 dump_field(str, db->magic_num, sizeof(db->magic_num));
509 pr_debug("%s:%d: db.magic_num: '%s'\n", func, line,
511 pr_debug("%s:%d: db.version: %u\n", func, line,
513 pr_debug("%s:%d: db.index_64: %u\n", func, line,
515 pr_debug("%s:%d: db.count_64: %u\n", func, line,
517 pr_debug("%s:%d: db.index_32: %u\n", func, line,
519 pr_debug("%s:%d: db.count_32: %u\n", func, line,
521 pr_debug("%s:%d: db.index_16: %u\n", func, line,
523 pr_debug("%s:%d: db.count_16: %u\n", func, line,
527 static void os_area_db_init(struct os_area_db *db)
530 HEADER_SIZE = offsetof(struct os_area_db, _db_data),
532 VALUES_64_COUNT = 57,
534 VALUES_32_COUNT = 57,
536 VALUES_16_COUNT = 57,
539 memset(db, 0, sizeof(struct os_area_db));
541 memcpy(db->magic_num, OS_AREA_DB_MAGIC_NUM, sizeof(db->magic_num));
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;
556 /* Rules to check db layout. */
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));
577 * update_flash_db - Helper for os_area_queue_work_handler.
581 static void update_flash_db(void)
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;
591 /* Read in header and db from flash. */
593 file = sys_open("/dev/ps3flash", O_RDWR, 0);
596 pr_debug("%s:%d sys_open failed\n", __func__, __LINE__);
600 header = kmalloc(buf_len, GFP_KERNEL);
603 pr_debug("%s:%d kmalloc failed\n", __func__, __LINE__);
607 offset = sys_lseek(file, 0, SEEK_SET);
610 pr_debug("%s:%d sys_lseek failed\n", __func__, __LINE__);
611 goto fail_header_seek;
614 count = sys_read(file, (char __user *)header, buf_len);
616 result = count < OS_AREA_SEGMENT_SIZE || verify_header(header)
617 || count < header->db_area_offset * OS_AREA_SEGMENT_SIZE;
620 pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
625 /* Now got a good db offset and some maybe good db data. */
627 db = (void*)header + header->db_area_offset * OS_AREA_SEGMENT_SIZE;
629 result = db_verify(db);
632 printk(KERN_NOTICE "%s:%d: Verify of flash database failed, "
633 "formatting.\n", __func__, __LINE__);
638 /* Now got good db data. */
640 db_set_64(db, &os_area_db_id_rtc_diff, saved_params.rtc_diff);
642 offset = sys_lseek(file, header->db_area_offset * OS_AREA_SEGMENT_SIZE,
645 if (offset != header->db_area_offset * OS_AREA_SEGMENT_SIZE) {
646 pr_debug("%s:%d sys_lseek failed\n", __func__, __LINE__);
650 count = sys_write(file, (const char __user *)db,
651 sizeof(struct os_area_db));
653 if (count < sizeof(struct os_area_db)) {
654 pr_debug("%s:%d sys_write failed\n", __func__, __LINE__);
668 * os_area_queue_work_handler - Asynchronous write handler.
670 * An asynchronous write for flash memory and the device tree. Do not
671 * call directly, use os_area_queue_work().
674 static void os_area_queue_work_handler(struct work_struct *work)
676 struct device_node *node;
678 pr_debug(" -> %s:%d\n", __func__, __LINE__);
680 node = of_find_node_by_path("/");
683 os_area_set_property(node, &property_rtc_diff);
686 pr_debug("%s:%d of_find_node_by_path failed\n",
689 #if defined(CONFIG_PS3_FLASH) || defined(CONFIG_PS3_FLASH_MODULE)
692 printk(KERN_WARNING "%s:%d: No flash rom driver configured.\n",
695 pr_debug(" <- %s:%d\n", __func__, __LINE__);
698 static void os_area_queue_work(void)
700 static DECLARE_WORK(q, os_area_queue_work_handler);
707 * ps3_os_area_save_params - Copy data from os area mirror to @saved_params.
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.
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().
720 void __init ps3_os_area_save_params(void)
725 struct os_area_header *header;
726 struct os_area_params *params;
727 struct os_area_db *db;
729 pr_debug(" -> %s:%d\n", __func__, __LINE__);
731 result = ps3_repository_read_boot_dat_info(&lpar_addr, &size);
734 pr_debug("%s:%d ps3_repository_read_boot_dat_info failed\n",
739 header = (struct os_area_header *)__va(lpar_addr);
740 params = (struct os_area_params *)__va(lpar_addr
741 + OS_AREA_SEGMENT_SIZE);
743 result = verify_header(header);
746 /* Second stage kernels exit here. */
747 pr_debug("%s:%d verify_header failed\n", __func__, __LINE__);
752 db = (struct os_area_db *)__va(lpar_addr
753 + header->db_area_offset * OS_AREA_SEGMENT_SIZE);
759 result = db_verify(db) || db_get_rtc_diff(db, &saved_params.rtc_diff);
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;
766 memset(header, 0, sizeof(*header));
768 pr_debug(" <- %s:%d\n", __func__, __LINE__);
772 * ps3_os_area_init - Setup os area device tree properties as needed.
775 void __init ps3_os_area_init(void)
777 struct device_node *node;
779 pr_debug(" -> %s:%d\n", __func__, __LINE__);
781 node = of_find_node_by_path("/");
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);
789 if(!saved_params.rtc_diff)
790 saved_params.rtc_diff = SECONDS_FROM_1970_TO_2000;
793 os_area_set_property(node, &property_rtc_diff);
794 os_area_set_property(node, &property_av_multi_out);
797 pr_debug("%s:%d of_find_node_by_path failed\n",
800 pr_debug(" <- %s:%d\n", __func__, __LINE__);
804 * ps3_os_area_get_rtc_diff - Returns the rtc diff value.
807 u64 ps3_os_area_get_rtc_diff(void)
809 return saved_params.rtc_diff;
813 * ps3_os_area_set_rtc_diff - Set the rtc diff value.
815 * An asynchronous write is needed to support writing updates from
816 * the timer interrupt context.
819 void ps3_os_area_set_rtc_diff(u64 rtc_diff)
821 if (saved_params.rtc_diff != rtc_diff) {
822 saved_params.rtc_diff = rtc_diff;
823 os_area_queue_work();
828 * ps3_os_area_get_av_multi_out - Returns the default video mode.
831 enum ps3_param_av_multi_out ps3_os_area_get_av_multi_out(void)
833 return saved_params.av_multi_out;
835 EXPORT_SYMBOL_GPL(ps3_os_area_get_av_multi_out);