summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_x1000/fiiom3k/installer-fiiom3k.c
blob: 10a58ace3893ba84060e7904255cddbd191e27d6 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 Aidan MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "installer-fiiom3k.h"
#include "nand-x1000.h"
#include "system.h"
#include "core_alloc.h"
#include "file.h"
#include "microtar.h"
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#define IMAGE_SIZE  (128 * 1024)
#define TAR_SIZE    (256 * 1024)

static int flash_prepare(void)
{
    int mf_id, dev_id;
    int rc;

    rc = nand_open();
    if(rc < 0)
        return INSTALL_ERR_FLASH(NAND_OPEN, rc);

    rc = nand_identify(&mf_id, &dev_id);
    if(rc < 0) {
        nand_close();
        return INSTALL_ERR_FLASH(NAND_IDENTIFY, rc);
    }

    return INSTALL_SUCCESS;
}

static void flash_finish(void)
{
    /* Ensure writes are always disabled when we finish.
     * Errors are safe to ignore here, there's nothing we could do anyway. */
    nand_enable_writes(false);
    nand_close();
}

static int flash_img_read(uint8_t* buffer)
{
    int rc = flash_prepare();
    if(rc < 0)
        goto error;

    rc = nand_read(0, IMAGE_SIZE, buffer);
    if(rc < 0) {
        rc = INSTALL_ERR_FLASH(NAND_READ, rc);
        goto error;
    }

  error:
    flash_finish();
    return rc;
}

static int flash_img_write(const uint8_t* buffer)
{
    int rc = flash_prepare();
    if(rc < 0)
        goto error;

    rc = nand_enable_writes(true);
    if(rc < 0) {
        rc = INSTALL_ERR_FLASH(NAND_ENABLE_WRITES, rc);
        goto error;
    }

    rc = nand_erase(0, IMAGE_SIZE);
    if(rc < 0) {
        rc = INSTALL_ERR_FLASH(NAND_ERASE, rc);
        goto error;
    }

    rc = nand_write(0, IMAGE_SIZE, buffer);
    if(rc < 0) {
        rc = INSTALL_ERR_FLASH(NAND_WRITE, rc);
        goto error;
    }

  error:
    flash_finish();
    return rc;
}

static int patch_img(mtar_t* tar, uint8_t* buffer, const char* filename,
                     size_t patch_offset, size_t patch_size)
{
    /* Seek to file */
    mtar_header_t h;
    int rc = mtar_find(tar, filename, &h);
    if(rc != MTAR_ESUCCESS) {
        rc = INSTALL_ERR_MTAR(TAR_FIND, rc);
        return rc;
    }

    /* We need a normal file */
    if(h.type != 0 && h.type != MTAR_TREG)
        return INSTALL_ERR_BAD_FORMAT;

    /* Check size does not exceed patch area */
    if(h.size > patch_size)
        return INSTALL_ERR_BAD_FORMAT;

    /* Read data directly into patch area, fill unused bytes with 0xff */
    memset(&buffer[patch_offset], 0xff, patch_size);
    rc = mtar_read_data(tar, &buffer[patch_offset], h.size);
    if(rc != MTAR_ESUCCESS) {
        rc = INSTALL_ERR_MTAR(TAR_READ, rc);
        return rc;
    }

    return INSTALL_SUCCESS;
}

int install_boot(const char* srcfile)
{
    int rc;
    mtar_t* tar = NULL;
    int handle = -1;

    /* Allocate enough memory for image and tar state */
    size_t bufsize = IMAGE_SIZE + sizeof(mtar_t) + 2*CACHEALIGN_SIZE;
    handle = core_alloc("boot_image", bufsize);
    if(handle < 0) {
        rc = INSTALL_ERR_OUT_OF_MEMORY;
        goto error;
    }

    uint8_t* buffer = core_get_data(handle);

    /* Tar state alloc */
    CACHEALIGN_BUFFER(buffer, bufsize);
    tar = (mtar_t*)buffer;
    memset(tar, 0, sizeof(tar));

    /* Image buffer alloc */
    buffer += sizeof(mtar_t);
    CACHEALIGN_BUFFER(buffer, bufsize);

    /* Read the flash -- we need an existing image to patch */
    rc = flash_img_read(buffer);
    if(rc < 0)
        goto error;

    /* Open the tarball */
    rc = mtar_open(tar, srcfile, "r");
    if(rc != MTAR_ESUCCESS) {
        rc = INSTALL_ERR_MTAR(TAR_OPEN, rc);
        goto error;
    }

    /* Extract the needed files & patch 'em in */
    rc = patch_img(tar, buffer, "spl.m3k", 0, 12 * 1024);
    if(rc < 0)
        goto error;

    rc = patch_img(tar, buffer, "bootloader.ucl", 0x6800, 102 * 1024);
    if(rc < 0)
        goto error;

    /* Flash the new image */
    rc = flash_img_write(buffer);
    if(rc < 0)
        goto error;

    rc = INSTALL_SUCCESS;

  error:
    if(tar && tar->close)
        mtar_close(tar);
    if(handle >= 0)
        core_free(handle);
    return rc;
}

int backup_boot(const char* destfile)
{
    int rc;
    int handle = -1;
    int fd = -1;
    size_t bufsize = IMAGE_SIZE + CACHEALIGN_SIZE - 1;
    handle = core_alloc("boot_image", bufsize);
    if(handle < 0) {
        rc = INSTALL_ERR_OUT_OF_MEMORY;
        goto error;
    }

    uint8_t* buffer = core_get_data(handle);
    CACHEALIGN_BUFFER(buffer, bufsize);

    rc = flash_img_read(buffer);
    if(rc < 0)
        goto error;

    fd = open(destfile, O_CREAT|O_TRUNC|O_WRONLY);
    if(fd < 0) {
        rc = INSTALL_ERR_FILE_IO;
        goto error;
    }

    ssize_t cnt = write(fd, buffer, IMAGE_SIZE);
    if(cnt != IMAGE_SIZE) {
        rc = INSTALL_ERR_FILE_IO;
        goto error;
    }

  error:
    if(fd >= 0)
        close(fd);
    if(handle >= 0)
        core_free(handle);
    return rc;
}

int restore_boot(const char* srcfile)
{
    int rc;
    int handle = -1;
    int fd = -1;
    size_t bufsize = IMAGE_SIZE + CACHEALIGN_SIZE - 1;
    handle = core_alloc("boot_image", bufsize);
    if(handle < 0) {
        rc = INSTALL_ERR_OUT_OF_MEMORY;
        goto error;
    }

    uint8_t* buffer = core_get_data(handle);
    CACHEALIGN_BUFFER(buffer, bufsize);

    fd = open(srcfile, O_RDONLY);
    if(fd < 0) {
        rc = INSTALL_ERR_FILE_NOT_FOUND;
        goto error;
    }

    off_t fsize = filesize(fd);
    if(fsize != IMAGE_SIZE) {
        rc = INSTALL_ERR_BAD_FORMAT;
        goto error;
    }

    ssize_t cnt = read(fd, buffer, IMAGE_SIZE);
    if(cnt != IMAGE_SIZE) {
        rc = INSTALL_ERR_FILE_IO;
        goto error;
    }

    close(fd);
    fd = -1;

    rc = flash_img_write(buffer);
    if(rc < 0)
        goto error;

  error:
    if(fd >= 0)
        close(fd);
    if(handle >= 0)
        core_free(handle);
    return rc;
}