2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/ioctl.h>
27 #include <ps3-flash.h>
31 #include "ui-system.h"
34 static const char flash_dev[] = "/dev/ps3flash";
35 static const char fb_dev[] = "/dev/fb0";
37 static const struct os_area_db_id id_default_item =
39 .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
42 static const struct os_area_db_id id_video_mode =
44 .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
45 .key = OS_AREA_DB_KEY_VIDEO_MODE, /* 2 */
47 static const struct os_area_db_id id_flags =
49 .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
52 static const struct os_area_db_id id_timeout =
54 .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
58 struct ps3_flash_ctx {
60 struct os_area_header header;
61 struct os_area_params params;
65 static void ps3_flash_close(struct ps3_flash_ctx *fc)
73 static int ps3_flash_open(struct ps3_flash_ctx *fc, const char *mode)
77 fc->dev = fopen(flash_dev, mode);
80 pb_log("%s: fopen failed: %s: %s\n", __func__, strerror(errno),
85 os_area_set_log_stream(pb_log_get_stream());
87 result = os_area_fixed_read(&fc->header, &fc->params, fc->dev);
90 pb_log("%s: os_area_fixed_read failed: %s\n", __func__);
102 * ps3_flash_get_values - Read values from the PS3 flash memory database.
104 * Returns zero on success.
107 int ps3_flash_get_values(struct ps3_flash_values *values)
111 struct ps3_flash_ctx fc;
114 result = ps3_flash_open(&fc, "r");
119 result = os_area_db_read(&fc.db, &fc.header, fc.dev);
121 ps3_flash_close(&fc);
124 pb_log("%s: os_area_db_read failed: %s\n", __func__,
129 sum = result = os_area_db_get(&fc.db, &id_default_item, &tmp);
132 values->default_item = (uint32_t)tmp;
134 result = os_area_db_get(&fc.db, &id_timeout, &tmp);
137 values->timeout = (uint8_t)tmp;
139 sum += result = os_area_db_get(&fc.db, &id_video_mode, &tmp);
142 values->video_mode = (uint16_t)tmp;
145 pb_log("%s: default_item: %x\n", __func__,
146 (unsigned int)values->default_item);
147 pb_log("%s: timeout: %u\n", __func__,
148 (unsigned int)values->timeout);
149 pb_log("%s: video_mode: %u\n", __func__,
150 (unsigned int)values->video_mode);
152 return (result || sum) ? -1 : 0;
156 * ps3_flash_set_values - Writes values from the PS3 flash memory database.
158 * Formats the flash database before writing if a valid database if not found.
159 * Returns zero on success.
162 int ps3_flash_set_values(const struct ps3_flash_values *values)
165 struct ps3_flash_ctx fc;
167 pb_log("%s: default_item: %u\n", __func__, values->default_item);
168 pb_log("%s: video_mode: %u\n", __func__, values->video_mode);
170 result = ps3_flash_open(&fc, "r+");
175 result = os_area_db_read(&fc.db, &fc.header, fc.dev);
178 pb_log("%s: os_area_db_read failed: %s\n", __func__,
180 pb_log("%s: formating db\n", __func__);
182 result = os_area_db_format(&fc.db, &fc.header, fc.dev);
185 pb_log("%s: db_format failed: %s\n", __func__,
191 /* timeout is currently read-only, set with ps3-bl-option */
193 result = os_area_db_set_32(&fc.db, &id_default_item,
194 values->default_item);
195 result += os_area_db_set_16(&fc.db, &id_video_mode,
198 result += os_area_db_write(&fc.db, &fc.header, fc.dev);
200 ps3_flash_close(&fc);
204 ps3_flash_close(&fc);
209 * ps3_video_ioctl - Low level ioctl helper.
211 * Use ps3_get_video_mode or ps3_set_video_mode().
214 static int ps3_video_ioctl(int request, unsigned int *mode_id)
219 fd = open(fb_dev, O_RDWR);
222 pb_log("%s: open failed: %s: %s\n", __func__, strerror(errno),
227 result = ioctl(fd, request, (unsigned long)mode_id);
232 pb_log("%s: ioctl failed: %s: %s\n", __func__, strerror(errno),
241 * ps3_set_video_mode - Set the PS3 video mode.
242 * @mode_id: The PS3 video mode_id as documented in the ps3-video-mode man page.
244 * Returns zero on success.
247 int ps3_set_video_mode(unsigned int mode_id)
249 pb_log("%s: %u\n", __func__, mode_id);
250 return ps3_video_ioctl(PS3FB_IOCTL_SETMODE, &mode_id);
254 * ps3_set_video_mode - Get the current PS3 video mode.
255 * @mode_id: The PS3 video mode_id as documented in the ps3-video-mode man page.
257 * Returns zero on success.
260 int ps3_get_video_mode(unsigned int *mode_id)
266 result = ps3_video_ioctl(PS3FB_IOCTL_GETMODE, mode_id);
268 pb_log("%s: %u\n", __func__, *mode_id);