summaryrefslogtreecommitdiffstats
path: root/bootloader/x1000/utils.c
blob: 837160a11342eb5d65a254cc82081cddc7908123 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2022 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 "x1000bootloader.h"
#include "core_alloc.h"
#include "storage.h"
#include "button.h"
#include "kernel.h"
#include "usb.h"
#include "file.h"
#include "rb-loader.h"
#include "loader_strerror.h"
#include "linuxboot.h"
#include "nand-x1000.h"

/* Set to true if a SYS_USB_CONNECTED event is seen
 * Set to false if a SYS_USB_DISCONNECTED event is seen
 * Handled by the gui code since that's how events are delivered
 * TODO: this is an ugly kludge */
bool is_usb_connected = false;

/* this is both incorrect and incredibly racy... */
int check_disk(bool wait)
{
    if(storage_present(IF_MD(0)))
        return DISK_PRESENT;
    if(!wait)
        return DISK_ABSENT;

    while(!storage_present(IF_MD(0))) {
        splashf(0, "Insert SD card\nPress " BL_QUIT_NAME " to cancel");
        if(get_button(HZ/4) == BL_QUIT)
            return DISK_CANCELED;
    }

    /* a lie intended to give time for mounting the disk in the background */
    splashf(HZ, "Scanning disk");

    return DISK_PRESENT;
}

void usb_mode(void)
{
    if(!is_usb_connected)
        splashf(0, "Waiting for USB\nPress " BL_QUIT_NAME " to cancel");

    while(!is_usb_connected)
        if(get_button(TIMEOUT_BLOCK) == BL_QUIT)
            return;

    splashf(0, "USB mode");
    usb_acknowledge(SYS_USB_CONNECTED_ACK);

    while(is_usb_connected)
        get_button(TIMEOUT_BLOCK);

    splashf(3*HZ, "USB disconnected");
}

int load_rockbox(const char* filename, size_t* sizep)
{
    if(check_disk(true) != DISK_PRESENT)
        return -1;

    int handle = core_alloc_maximum("rockbox", sizep, &buflib_ops_locked);
    if(handle < 0) {
        splashf(5*HZ, "Out of memory");
        return -2;
    }

    unsigned char* loadbuffer = core_get_data(handle);
    int rc = load_firmware(loadbuffer, filename, *sizep);
    if(rc <= 0) {
        core_free(handle);
        splashf(5*HZ, "Error loading Rockbox\n%s", loader_strerror(rc));
        return -3;
    }

    core_shrink(handle, loadbuffer, rc);
    *sizep = rc;

    return handle;
}

int load_uimage_file(const char* filename,
                     struct uimage_header* uh, size_t* sizep)
{
    if(check_disk(true) != DISK_PRESENT)
        return -1;

    int fd = open(filename, O_RDONLY);
    if(fd < 0) {
        splashf(5*HZ, "Can't open file\n%s", filename);
        return -2;
    }

    int handle = uimage_load(uh, sizep, uimage_fd_reader, (void*)(intptr_t)fd);
    if(handle <= 0) {
        splashf(5*HZ, "Cannot load uImage (%d)\n%s", handle, filename);
        return -3;
    }

    return handle;
}

struct nand_reader_data
{
    nand_drv* ndrv;
    uint32_t addr;
    uint32_t end_addr;
};

static ssize_t uimage_nand_reader(void* buf, size_t count, void* rctx)
{
    struct nand_reader_data* d = rctx;

    if(d->addr + count > d->end_addr)
        count = d->end_addr - d->addr;

    int ret = nand_read_bytes(d->ndrv, d->addr, count, buf);
    if(ret != NAND_SUCCESS)
        return -1;

    d->addr += count;
    return count;
}

int load_uimage_flash(uint32_t addr, uint32_t length,
                      struct uimage_header* uh, size_t* sizep)
{
    int handle = -1;

    struct nand_reader_data n;
    n.ndrv = nand_init();
    n.addr = addr;
    n.end_addr = addr + length;

    nand_lock(n.ndrv);
    if(nand_open(n.ndrv) != NAND_SUCCESS) {
        splashf(5*HZ, "NAND open failed");
        nand_unlock(n.ndrv);
        return -1;
    }

    handle = uimage_load(uh, sizep, uimage_nand_reader, &n);

    nand_close(n.ndrv);
    nand_unlock(n.ndrv);

    if(handle <= 0) {
        splashf(5*HZ, "uImage load failed (%d)", handle);
        return -2;
    }

    return handle;
}

int dump_flash(int fd, uint32_t addr, uint32_t length)
{
    static char buf[8192];
    int ret = 0;

    nand_drv* ndrv = nand_init();
    nand_lock(ndrv);

    ret = nand_open(ndrv);
    if(ret != NAND_SUCCESS) {
        splashf(5*HZ, "NAND open failed\n");
        nand_unlock(ndrv);
        return ret;
    }

    while(length > 0) {
        uint32_t count = MIN(length, sizeof(buf));
        ret = nand_read_bytes(ndrv, addr, count, buf);
        if(ret != NAND_SUCCESS) {
            splashf(5*HZ, "Dump failed\nNAND I/O error");
            goto out;
        }

        if(write(fd, buf, count) != (ssize_t)count) {
            splashf(5*HZ, "Dump failed\nFile I/O error");
            ret = -1;
            goto out;
        }

        length -= count;
        addr += count;
    }

  out:
    nand_close(ndrv);
    nand_unlock(ndrv);
    return ret;
}

int dump_flash_file(const char* file, uint32_t addr, uint32_t length)
{
    if(check_disk(true) != DISK_PRESENT)
        return -1;

    splashf(0, "Dumping...\n%s\n0x%08lx\n%lu bytes", file, addr, length);

    int fd = open(file, O_WRONLY|O_CREAT|O_TRUNC);
    if(fd < 0) {
        splashf(5*HZ, "Cannot open file\n%s", file);
        return -2;
    }

    int rc = dump_flash(fd, addr, length);
    if(rc < 0) {
        close(fd);
        remove(file);
        return -3;
    }

    splashf(5*HZ, "Dumped\n%s", file);
    close(fd);
    return 0;
}

void dump_of_player(void)
{
#ifdef OF_PLAYER_ADDR
    dump_flash_file("/of_player.img", OF_PLAYER_ADDR, OF_PLAYER_LENGTH);
#endif
}

void dump_of_recovery(void)
{
#ifdef OF_RECOVERY_ADDR
    dump_flash_file("/of_recovery.img", OF_RECOVERY_ADDR, OF_RECOVERY_LENGTH);
#endif
}

void dump_entire_flash(void)
{
#if defined(FIIO_M3K) || defined(SHANLING_Q1) || defined(EROS_QN)
    /* TODO: this should read the real chip size instead of hardcoding it */
    dump_flash_file("/flash.img", 0, 2048 * 64 * 1024);
#endif
}