summaryrefslogtreecommitdiff
path: root/firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2021-07-11 02:32:05 +0100
committerAidan MacDonald <amachronic@protonmail.com>2021-07-11 15:39:50 +0100
commit84362141a0d1e33b29d12162caba7537dd5684b8 (patch)
treed966b33e6049cc54abe04d9b66ef0db794ebe948 /firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c
parente9d228832ccbee7e486c45a51680c6f94ce1ab92 (diff)
downloadrockbox-84362141a0d1e33b29d12162caba7537dd5684b8.tar.gz
rockbox-84362141a0d1e33b29d12162caba7537dd5684b8.zip
x1000: Unified flash bootloader installer
Change-Id: Ib1d41d4e7d663ff8a21eb08108c13568f7408533
Diffstat (limited to 'firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c')
-rw-r--r--firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c253
1 files changed, 0 insertions, 253 deletions
diff --git a/firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c b/firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c
deleted file mode 100644
index 8ce73bf09e..0000000000
--- a/firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c
+++ /dev/null
@@ -1,253 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "installer-fiiom3k.h"
23#include "nand-x1000.h"
24#include "system.h"
25#include "core_alloc.h"
26#include "file.h"
27#include "microtar.h"
28#include <stdint.h>
29#include <string.h>
30#include <stdio.h>
31
32#define IMAGE_SIZE (128 * 1024)
33#define TAR_SIZE (256 * 1024)
34
35static int flash_img_read(uint8_t* buffer)
36{
37 nand_drv* drv = nand_init();
38 nand_lock(drv);
39
40 int rc = nand_open(drv);
41 if(rc < 0)
42 goto error;
43
44 rc = nand_read_bytes(drv, 0, IMAGE_SIZE, buffer);
45 if(rc < 0) {
46 rc = INSTALL_ERR_FLASH(NAND_READ, rc);
47 goto error;
48 }
49
50 error:
51 nand_close(drv);
52 nand_unlock(drv);
53 return rc;
54}
55
56static int flash_img_write(const uint8_t* buffer)
57{
58 nand_drv* drv = nand_init();
59 nand_lock(drv);
60
61 int rc = nand_open(drv);
62 if(rc < 0)
63 goto error;
64
65 rc = nand_write_bytes(drv, 0, IMAGE_SIZE, buffer);
66 if(rc < 0) {
67 rc = INSTALL_ERR_FLASH(NAND_WRITE, rc);
68 goto error;
69 }
70
71 error:
72 nand_close(drv);
73 nand_unlock(drv);
74 return rc;
75}
76
77static int patch_img(mtar_t* tar, uint8_t* buffer, const char* filename,
78 size_t patch_offset, size_t patch_size)
79{
80 /* Seek to file */
81 mtar_header_t h;
82 int rc = mtar_find(tar, filename, &h);
83 if(rc != MTAR_ESUCCESS) {
84 rc = INSTALL_ERR_MTAR(TAR_FIND, rc);
85 return rc;
86 }
87
88 /* We need a normal file */
89 if(h.type != 0 && h.type != MTAR_TREG)
90 return INSTALL_ERR_BAD_FORMAT;
91
92 /* Check size does not exceed patch area */
93 if(h.size > patch_size)
94 return INSTALL_ERR_BAD_FORMAT;
95
96 /* Read data directly into patch area, fill unused bytes with 0xff */
97 memset(&buffer[patch_offset], 0xff, patch_size);
98 rc = mtar_read_data(tar, &buffer[patch_offset], h.size);
99 if(rc != MTAR_ESUCCESS) {
100 rc = INSTALL_ERR_MTAR(TAR_READ, rc);
101 return rc;
102 }
103
104 return INSTALL_SUCCESS;
105}
106
107int install_boot(const char* srcfile)
108{
109 int rc;
110 mtar_t* tar = NULL;
111 int handle = -1;
112
113 /* Allocate enough memory for image and tar state */
114 size_t bufsize = IMAGE_SIZE + sizeof(mtar_t) + 2*CACHEALIGN_SIZE;
115 handle = core_alloc("boot_image", bufsize);
116 if(handle < 0) {
117 rc = INSTALL_ERR_OUT_OF_MEMORY;
118 goto error;
119 }
120
121 uint8_t* buffer = core_get_data(handle);
122
123 /* Tar state alloc */
124 CACHEALIGN_BUFFER(buffer, bufsize);
125 tar = (mtar_t*)buffer;
126 memset(tar, 0, sizeof(tar));
127
128 /* Image buffer alloc */
129 buffer += sizeof(mtar_t);
130 CACHEALIGN_BUFFER(buffer, bufsize);
131
132 /* Read the flash -- we need an existing image to patch */
133 rc = flash_img_read(buffer);
134 if(rc < 0)
135 goto error;
136
137 /* Open the tarball */
138 rc = mtar_open(tar, srcfile, "r");
139 if(rc != MTAR_ESUCCESS) {
140 rc = INSTALL_ERR_MTAR(TAR_OPEN, rc);
141 goto error;
142 }
143
144 /* Extract the needed files & patch 'em in */
145 rc = patch_img(tar, buffer, "spl.m3k", 0, 12 * 1024);
146 if(rc < 0)
147 goto error;
148
149 rc = patch_img(tar, buffer, "bootloader.ucl", 0x6800, 102 * 1024);
150 if(rc < 0)
151 goto error;
152
153 /* Flash the new image */
154 rc = flash_img_write(buffer);
155 if(rc < 0)
156 goto error;
157
158 rc = INSTALL_SUCCESS;
159
160 error:
161 if(tar && tar->close)
162 mtar_close(tar);
163 if(handle >= 0)
164 core_free(handle);
165 return rc;
166}
167
168int backup_boot(const char* destfile)
169{
170 int rc;
171 int handle = -1;
172 int fd = -1;
173 size_t bufsize = IMAGE_SIZE + CACHEALIGN_SIZE - 1;
174 handle = core_alloc("boot_image", bufsize);
175 if(handle < 0) {
176 rc = INSTALL_ERR_OUT_OF_MEMORY;
177 goto error;
178 }
179
180 uint8_t* buffer = core_get_data(handle);
181 CACHEALIGN_BUFFER(buffer, bufsize);
182
183 rc = flash_img_read(buffer);
184 if(rc < 0)
185 goto error;
186
187 fd = open(destfile, O_CREAT|O_TRUNC|O_WRONLY);
188 if(fd < 0) {
189 rc = INSTALL_ERR_FILE_IO;
190 goto error;
191 }
192
193 ssize_t cnt = write(fd, buffer, IMAGE_SIZE);
194 if(cnt != IMAGE_SIZE) {
195 rc = INSTALL_ERR_FILE_IO;
196 goto error;
197 }
198
199 error:
200 if(fd >= 0)
201 close(fd);
202 if(handle >= 0)
203 core_free(handle);
204 return rc;
205}
206
207int restore_boot(const char* srcfile)
208{
209 int rc;
210 int handle = -1;
211 int fd = -1;
212 size_t bufsize = IMAGE_SIZE + CACHEALIGN_SIZE - 1;
213 handle = core_alloc("boot_image", bufsize);
214 if(handle < 0) {
215 rc = INSTALL_ERR_OUT_OF_MEMORY;
216 goto error;
217 }
218
219 uint8_t* buffer = core_get_data(handle);
220 CACHEALIGN_BUFFER(buffer, bufsize);
221
222 fd = open(srcfile, O_RDONLY);
223 if(fd < 0) {
224 rc = INSTALL_ERR_FILE_NOT_FOUND;
225 goto error;
226 }
227
228 off_t fsize = filesize(fd);
229 if(fsize != IMAGE_SIZE) {
230 rc = INSTALL_ERR_BAD_FORMAT;
231 goto error;
232 }
233
234 ssize_t cnt = read(fd, buffer, IMAGE_SIZE);
235 if(cnt != IMAGE_SIZE) {
236 rc = INSTALL_ERR_FILE_IO;
237 goto error;
238 }
239
240 close(fd);
241 fd = -1;
242
243 rc = flash_img_write(buffer);
244 if(rc < 0)
245 goto error;
246
247 error:
248 if(fd >= 0)
249 close(fd);
250 if(handle >= 0)
251 core_free(handle);
252 return rc;
253}