summaryrefslogtreecommitdiffstats
path: root/lib/flash.c
blob: 4780847206fbf5a76ad88e576506d7fa70de25ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 *  PS3 flash memory os area.
 *
 *  Copyright (C) 2006 Sony Computer Entertainment Inc.
 *  Copyright 2006 Sony Corp.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "ps3-flash.h"

#if defined(DEBUG)
#define DBG os_area_log
#else
static inline int __attribute__ ((format (printf, 1, 2))) DBG(
	__attribute__((unused)) const char *fmt, ...) {return 0;}
#endif

static FILE *osa_log;

/**
 * os_area_set_log_stream - Set the message logging stream.
 * @stream: A file stream to log messages to.
 *
 * The default log stream is stdout.  Returns the old
 * stream.
 */

FILE *os_area_set_log_stream(FILE *log)
{
	FILE *old = osa_log ? osa_log : stdout;

	assert(log);

	fflush(log);
	fflush(old);
		
	osa_log = log;
	return old;
}

int os_area_log(const char *fmt, ...)
{
	int ret;
	va_list ap;
	FILE *f;

	f = osa_log ? osa_log : stdout;

	va_start(ap, fmt);
	ret = vfprintf(f, fmt, ap);
	va_end(ap);
	fflush(f);

	return ret;
}

/**
 * os_area_header_read - Read the firmware defined header.
 */

int os_area_header_read(struct os_area_header *h, FILE *dev)
{
	int result;
	size_t bytes;

	if (!dev) {
		DBG("%s:%d: bad stream\n", __func__, __LINE__);
		return -1;
	}

	result = fseek(dev, 0, SEEK_SET);

	if (result) {
		os_area_log("%s: seek error: os_area_header: %s\n", __func__,
			strerror(errno));
		return result;
	}

	bytes = fread(h, 1, sizeof(struct os_area_header), dev);

	if (bytes < sizeof(struct os_area_header)) {
		os_area_log("%s: read error: os_area_header: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	result = os_area_header_verify(h);

	if (result) {
		os_area_log("%s: invalid os_area_header\n", __func__);
		return -1;
	}

	return 0;
}

/**
 * os_area_header_write - Write the firmware defined header.
 */

int os_area_header_write(const struct os_area_header *h, FILE *dev)
{
	int result;
	size_t bytes;

	if (!dev) {
		DBG("%s:%d: bad stream\n", __func__, __LINE__);
		return -1;
	}

	result = os_area_header_verify(h);

	if (result) {
		os_area_log("%s: invalid os_area_header\n", __func__);
		return -1;
	}

	result = fseek(dev, 0, SEEK_SET);

	if (result) {
		os_area_log("%s: seek error: os_area_header: %s\n", __func__,
			strerror(errno));
		return result;
	}

	bytes = fwrite(h, 1, sizeof(struct os_area_header), dev);

	if (bytes < sizeof(struct os_area_header)) {
		os_area_log("%s: fwrite error: os_area_header: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	return 0;
}

/**
 * os_area_params_read - Read the firmware defined params.
 */

int os_area_params_read(struct os_area_params *p, FILE *dev)
{
	int result;
	size_t bytes;

	if (!dev) {
		DBG("%s:%d: bad stream\n", __func__, __LINE__);
		return -1;
	}

	result = fseek(dev, OS_AREA_SEGMENT_SIZE, SEEK_SET);

	if (result) {
		os_area_log("%s: seek error: os_area_params: %s\n", __func__,
			strerror(errno));
		return result;
	}

	bytes = fread(p, 1, sizeof(struct os_area_params), dev);

	if (bytes < sizeof(struct os_area_params)) {
		os_area_log("%s: read error: os_area_params: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	return 0;
}

/**
 * os_area_params_write - Write the firmware defined params.
 */

int os_area_params_write(const struct os_area_params *p, FILE *dev)
{
	int result;
	size_t bytes;

	if (!dev) {
		DBG("%s:%d: bad stream\n", __func__, __LINE__);
		return -1;
	}

	result = fseek(dev, OS_AREA_SEGMENT_SIZE, SEEK_SET);

	if (result) {
		os_area_log("%s: seek error: os_area_params: %s\n", __func__,
			strerror(errno));
		return result;
	}

	bytes = fwrite(p, 1, sizeof(struct os_area_params), dev);

	if (bytes < sizeof(struct os_area_params)) {
		os_area_log("%s: fwrite error: os_area_params: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	return 0;
}

/**
 * os_area_image_write - Write the other os boot image.
 */

int os_area_image_write(FILE *image, struct os_area_header *h, FILE *dev)
{
	int result;
	size_t flash_size;
	size_t image_size;
	size_t count;
	void* buf;

	result = fseek(dev, 0, SEEK_END);

	if (result) {
		os_area_log("%s: fseek dev failed: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	flash_size = ftell(dev) - h->ldr_area_offset * OS_AREA_SEGMENT_SIZE;

	DBG("%s:%d: flash_size %lxh\n", __func__, __LINE__,
		(unsigned long)flash_size);

	result = fseek(image, 0, SEEK_END);

	if (result) {
		os_area_log("%s: fseek image failed: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	image_size = ftell(image);

	DBG("%s:%d: image_size %lxh\n", __func__, __LINE__,
		(unsigned long)image_size);

	if (image_size > flash_size) {
		os_area_log("%s: image too big (%lxh / %lxh bytes).\n",
			__func__, (unsigned long)image_size,
			(unsigned long)flash_size);
		return -1;
	}

	buf = malloc(image_size);

	if (!buf) {
		DBG("%s: malloc failed: %s\n", __func__, strerror(errno));
		return -1;
	}

	rewind(image);
	count = fread(buf, image_size, 1, image);

	if (count != 1) {
		os_area_log("%s: fread failed: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	result = fseek(dev, h->ldr_area_offset * OS_AREA_SEGMENT_SIZE,
		SEEK_SET);

	if (result) {
		os_area_log("%s: seek error ldr_area_offset: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	count = fwrite(buf, image_size, 1, dev);

	if (count != 1) {
		os_area_log("%s: fwrite error image: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	result = os_area_set_ldr_size(h, dev, image_size);

	if (result) {
		os_area_log("%s: os_area_set_ldr_size failed: %s\n", __func__,
			strerror(errno));
		return -1;
	}

	os_area_log("wrote image to flash (%lxh / %lxh bytes).\n",
		(unsigned long)image_size, (unsigned long)flash_size);

	free(buf);

	return result;
}

/**
 * os_area_header_verify - Verify the firmware defined header.
 */

int os_area_header_verify(const struct os_area_header *h)
{
	if (memcmp(h->magic_num, "cell_ext_os_area", 16)) {
		os_area_log("%s: magic_num failed\n", __func__);
		return -1;
	}

	if (h->hdr_version < 1) {
		os_area_log("%s: hdr_version failed\n", __func__);
		return -1;
	}

	if (h->db_area_offset > h->ldr_area_offset) {
		os_area_log("%s: offsets failed\n", __func__);
		return -1;
	}

	return 0;
}

/**
 * os_area_set_ldr_format - Set the data format of the other os boot image.
 * @value: Specifies the format of the os boot image, one of enum ldr_format.
 */

int os_area_set_ldr_format(struct os_area_header *h, FILE *dev,
	enum os_area_ldr_format value)
{
	int result;

	DBG("%s:%d: %u(%xh) -> %u(%xh)\n", __func__, __LINE__,
		(unsigned int)h->ldr_format, (unsigned int)h->ldr_format,
		value, value);

	if (h->ldr_format == (uint32_t)value)
		return 0;

	h->ldr_format = (uint32_t)value ? HEADER_LDR_FORMAT_GZIP
		: HEADER_LDR_FORMAT_RAW; /* for now just flatten to gzip */

	os_area_header_write(h, dev);
	result = os_area_header_read(h, dev);

	if (result)
		os_area_log("%s: os_area_set_ldr_format failed\n", __func__);

	return result;
}

/**
 * os_area_set_ldr_size - Set the size in bytes of the other os boot image.
 * @value: Size in bytes.
 */

int os_area_set_ldr_size(struct os_area_header *h, FILE *dev,
	unsigned int value)
{
	int result;

	DBG("%s:%d: %u(%xh) -> %u(%xh)\n", __func__, __LINE__,
		(unsigned int)h->ldr_size, (unsigned int)h->ldr_size,
		value, value);

	if (h->ldr_size == (uint32_t)value)
		return 0;

	h->ldr_size = (uint32_t)value;

	os_area_header_write(h, dev);
	result = os_area_header_read(h, dev);

	if (result)
		os_area_log("%s: os_area_set_ldr_format failed\n", __func__);

	return result;
}

/**
 * os_area_set_boot_flag - Set the system boot flag.
 * @value: Boot flag value, one of enum os_area_boot_flag.
 */

int os_area_set_boot_flag(struct os_area_params *p, FILE *dev,
	enum os_area_boot_flag value)
{
	int result;

	DBG("%s:%d: %u(%xh) -> %u(%xh)\n", __func__, __LINE__,
		(unsigned int)p->boot_flag, (unsigned int)p->boot_flag,
		value, value);

	if (p->boot_flag == (uint32_t)value)
		return 0;

	p->boot_flag = (uint32_t)value ? PARAM_BOOT_FLAG_OTHER_OS
		: PARAM_BOOT_FLAG_GAME_OS; /* for now just flatten to otheros */

	os_area_params_write(p, dev);
	result = os_area_params_read(p, dev);

	if (result)
		os_area_log("%s: os_area_set_boot_flag failed\n", __func__);

	return result;
}

void os_area_header_dump(const struct os_area_header *h, const char *header,
	int id)
{
	os_area_log("%s:%d: h.magic_num:      '%s'\n",
		header, id,
		h->magic_num);
	os_area_log("%s:%d: h.hdr_version:     %u\n",
		header, id,
		h->hdr_version);
	os_area_log("%s:%d: h.db_area_offset:  %u\n",
		header, id,
		h->db_area_offset);
	os_area_log("%s:%d: h.ldr_area_offset: %u\n",
		header, id,
		h->ldr_area_offset);
	os_area_log("%s:%d: h.ldr_format:      %u (%s)\n",
		header, id,
		h->ldr_format, (h->ldr_format ? "gzip" : "raw"));
	os_area_log("%s:%d: h.ldr_size:        %u (%xh)\n",
		header, id,
		h->ldr_size, h->ldr_size);
}

void os_area_params_dump(const struct os_area_params *p, const char *header,
	int id)
{
	os_area_log("%s:%d: p.boot_flag:       %u (%s)\n",
		header, id,
		p->boot_flag, (p->boot_flag ? "other-os" : "game-os"));
	os_area_log("%s:%d: p.num_params:      %u\n",
		header, id,
		p->num_params);
	os_area_log("%s:%d: p.rtc_diff         %lld\n",
		header, id,
		(long long)p->rtc_diff);
	os_area_log("%s:%d: p.av_multi_out     %u\n",
		header, id,
		p->av_multi_out);
	os_area_log("%s:%d: p.ctrl_button:     %u\n",
		header, id,
		p->ctrl_button);
	os_area_log("%s:%d: p.static_ip_addr:  %u.%u.%u.%u\n",
		header, id,
		p->static_ip_addr[0], p->static_ip_addr[1],
		p->static_ip_addr[2], p->static_ip_addr[3]);
	os_area_log("%s:%d: p.network_mask:    %u.%u.%u.%u\n",
		header, id,
		p->network_mask[0], p->network_mask[1],
		p->network_mask[2], p->network_mask[3]);
	os_area_log("%s:%d: p.default_gateway: %u.%u.%u.%u\n",
		header, id,
		p->default_gateway[0], p->default_gateway[1],
		p->default_gateway[2], p->default_gateway[3]);
	os_area_log("%s:%d: p.dns_primary:     %u.%u.%u.%u\n",
		header, id,
		p->dns_primary[0], p->dns_primary[1],
		p->dns_primary[2], p->dns_primary[3]);
	os_area_log("%s:%d: p.dns_secondary:   %u.%u.%u.%u\n",
		header, id,
		p->dns_secondary[0], p->dns_secondary[1],
		p->dns_secondary[2], p->dns_secondary[3]);
}