]> git.ozlabs.org Git - petitboot/blob - ui/common/ps3.c
ui: callback & boot actions: kexec -> boot
[petitboot] / ui / common / ps3.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
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.
8  *
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.
13  *
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
17  */
18
19 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 #include <ps3-flash.h>
28 #include <ps3-av.h>
29
30 #include "log/log.h"
31 #include "ui-system.h"
32 #include "ps3.h"
33
34 static const char flash_dev[] = "/dev/ps3flash";
35 static const char fb_dev[] = "/dev/fb0";
36
37 static const struct os_area_db_id id_default_item =
38 {
39         .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
40         .key = 1,
41 };
42 static const struct os_area_db_id id_video_mode =
43 {
44         .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
45         .key = OS_AREA_DB_KEY_VIDEO_MODE,    /* 2 */
46 };
47 static const struct os_area_db_id id_flags =
48 {
49         .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
50         .key = 3,
51 };
52 static const struct os_area_db_id id_timeout =
53 {
54         .owner = OS_AREA_DB_OWNER_PETITBOOT, /* 3 */
55         .key = 4,
56 };
57
58 struct ps3_flash_ctx {
59         FILE *dev;
60         struct os_area_header header;
61         struct os_area_params params;
62         struct os_area_db db;
63 };
64
65 static void ps3_flash_close(struct ps3_flash_ctx *fc)
66 {
67         assert(fc->dev);
68
69         fclose(fc->dev);
70         fc->dev = NULL;
71 }
72
73 static int ps3_flash_open(struct ps3_flash_ctx *fc, const char *mode)
74 {
75         int result;
76
77         fc->dev = fopen(flash_dev, mode);
78
79         if (!fc->dev) {
80                 pb_log("%s: fopen failed: %s: %s\n", __func__, strerror(errno),
81                         flash_dev);
82                 return -1;
83         }
84
85         os_area_set_log_stream(pb_log_get_stream());
86
87         result = os_area_fixed_read(&fc->header, &fc->params, fc->dev);
88
89         if (result) {
90                 pb_log("%s: os_area_fixed_read failed\n", __func__);
91                 goto fail;
92         }
93
94         return 0;
95
96 fail:
97         ps3_flash_close(fc);
98         return -1;
99 }
100
101 /**
102  * ps3_flash_get_values - Read values from the PS3 flash memory database.
103  *
104  * Returns zero on success.
105  */
106
107 int ps3_flash_get_values(struct ps3_flash_values *values)
108 {
109         int result;
110         int sum;
111         struct ps3_flash_ctx fc;
112         uint64_t tmp;
113
114         result = ps3_flash_open(&fc, "r");
115
116         if (result)
117                 goto fail;
118
119         result = os_area_db_read(&fc.db, &fc.header, fc.dev);
120
121         ps3_flash_close(&fc);
122
123         if (result) {
124                 pb_log("%s: os_area_db_read failed: %s\n", __func__,
125                         strerror(errno));
126                 goto fail;
127         }
128
129         sum = result = os_area_db_get(&fc.db, &id_default_item, &tmp);
130
131         if (!result)
132                 values->default_item = (uint32_t)tmp;
133
134         result = os_area_db_get(&fc.db, &id_timeout, &tmp);
135
136         if (!result)
137                 values->timeout = (uint8_t)tmp;
138
139         sum += result = os_area_db_get(&fc.db, &id_video_mode, &tmp);
140
141         if (!result)
142                 values->video_mode = (uint16_t)tmp;
143
144         pb_log("%s: default_item: %x\n", __func__,
145                 (unsigned int)values->default_item);
146         pb_log("%s: timeout: %u\n", __func__,
147                 (unsigned int)values->timeout);
148         pb_log("%s: video_mode:   %u\n", __func__,
149                 (unsigned int)values->video_mode);
150 fail:
151         return (result || sum) ? -1 : 0;
152 }
153
154 /**
155  * ps3_flash_set_values - Writes values from the PS3 flash memory database.
156  *
157  * Formats the flash database before writing if a valid database if not found.
158  * Returns zero on success.
159  */
160
161 int ps3_flash_set_values(const struct ps3_flash_values *values)
162 {
163         int result;
164         struct ps3_flash_ctx fc;
165
166         pb_log("%s: default_item: %u\n", __func__, values->default_item);
167         pb_log("%s: video_mode:   %u\n", __func__, values->video_mode);
168
169         result = ps3_flash_open(&fc, "r+");
170
171         if (result)
172                 return result;
173
174         result = os_area_db_read(&fc.db, &fc.header, fc.dev);
175
176         if (result) {
177                 pb_log("%s: os_area_db_read failed: %s\n", __func__,
178                         strerror(errno));
179                 pb_log("%s: formating db\n", __func__);
180
181                 result = os_area_db_format(&fc.db, &fc.header, fc.dev);
182
183                 if (result) {
184                         pb_log("%s: db_format failed: %s\n", __func__,
185                                 strerror(errno));
186                         goto fail;
187                 }
188         }
189
190         /* timeout is currently read-only, set with ps3-bl-option */
191
192         result = os_area_db_set_32(&fc.db, &id_default_item,
193                 values->default_item);
194         result += os_area_db_set_16(&fc.db, &id_video_mode,
195                 values->video_mode);
196
197         result += os_area_db_write(&fc.db, &fc.header, fc.dev);
198
199         ps3_flash_close(&fc);
200         return result;
201
202 fail:
203         ps3_flash_close(&fc);
204         return -1;
205 }
206
207 /**
208  * ps3_video_ioctl - Low level ioctl helper.
209  *
210  * Use ps3_get_video_mode or ps3_set_video_mode().
211  */
212
213 static int ps3_video_ioctl(int request, unsigned int *mode_id)
214 {
215         int result;
216         int fd;
217
218         fd = open(fb_dev, O_RDWR);
219
220         if (fd < 0) {
221                 pb_log("%s: open failed: %s: %s\n", __func__, strerror(errno),
222                         fb_dev);
223                 return -1;
224         }
225
226         result = ioctl(fd, request, (unsigned long)mode_id);
227
228         close(fd);
229
230         if (result < 0) {
231                 pb_log("%s: ioctl failed: %s: %s\n", __func__, strerror(errno),
232                         fb_dev);
233                 return -1;
234         }
235
236         return 0;
237 }
238
239 /**
240  * ps3_set_video_mode - Set the PS3 video mode.
241  * @mode_id: The PS3 video mode_id as documented in the ps3-video-mode man page.
242  *
243  * Returns zero on success.
244  */
245
246 int ps3_set_video_mode(unsigned int mode_id)
247 {
248         pb_log("%s: %u\n", __func__, mode_id);
249         return ps3_video_ioctl(PS3FB_IOCTL_SETMODE, &mode_id);
250 }
251
252 /**
253  * ps3_set_video_mode - Get the current PS3 video mode.
254  * @mode_id: The PS3 video mode_id as documented in the ps3-video-mode man page.
255  *
256  * Returns zero on success.
257  */
258
259 int ps3_get_video_mode(unsigned int *mode_id)
260 {
261         int result;
262
263         *mode_id = 0;
264
265         result =  ps3_video_ioctl(PS3FB_IOCTL_GETMODE, mode_id);
266
267         pb_log("%s: %u\n", __func__, *mode_id);
268         return result;
269 }