summaryrefslogtreecommitdiffstats
path: root/lib/x1000-installer/src/xf_update.c
blob: 5a7c3b0430db16381ac05742faef161326bd6af8 (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
/***************************************************************************
 *             __________               __   ___.
 *   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 "xf_update.h"
#include "xf_error.h"
#include "file.h"
#include "md5.h"
#include <string.h>

static int process_stream(struct xf_nandio* nio,
                          struct xf_map* map,
                          struct xf_stream* stream)
{
    void* buffer;
    size_t count;
    int rc;

    /* calculate MD5 on the fly if taking a backup */
    md5_context md5_ctx;
    if(nio->mode == XF_NANDIO_READ)
        md5_starts(&md5_ctx);

    /* first deal with the file data */
    size_t bytes_left = map->length;
    while(bytes_left > 0) {
        count = bytes_left;
        rc = xf_nandio_get_buffer(nio, &buffer, &count);
        if(rc)
            return rc;

        if(nio->mode == XF_NANDIO_READ) {
            md5_update(&md5_ctx, buffer, count);
            rc = xf_stream_write(stream, buffer, count);
        } else {
            rc = xf_stream_read(stream, buffer, count);
        }

        bytes_left -= count;

        if(rc < 0 || (size_t)rc > count)
            return XF_E_IO;

        if((size_t)rc < count) {
            /* backup - we could not write all the data */
            if(nio->mode == XF_NANDIO_READ)
                return XF_E_IO;

            /* update - clear rest of buffer to 0xff */
            memset(buffer + rc, 0xff, count - rc);
            break;
        }
    }

    /* if updating - write blanks to the remainder of the region */
    while(bytes_left > 0) {
        count = bytes_left;
        rc = xf_nandio_get_buffer(nio, &buffer, &count);
        if(rc)
            return rc;

        memset(buffer, 0xff, count);
        bytes_left -= count;
    }

    /* finalize the MD5 sum */
    if(nio->mode == XF_NANDIO_READ) {
        md5_finish(&md5_ctx, map->md5);
        map->file_length = map->length;
        map->flags |= XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH;
    }

    return XF_E_SUCCESS;
}

static int process_map(struct xf_nandio* nio, struct xf_map* map, int map_size,
                       xf_update_open_stream_cb open_stream, void* os_arg)
{
    int rc, rc2;
    struct xf_stream stream;

    /* ensure the map is sequential and non-overlapping before continuing */
    if(xf_map_validate(map, map_size) != 0)
        return XF_E_INVALID_PARAMETER;

    for(int i = 0; i < map_size; ++i) {
        /* seek to initial offset */
        rc = xf_nandio_seek(nio, map[i].offset);
        if(rc)
            return rc;

        rc = open_stream(os_arg, map[i].name, &stream);
        if(rc)
            return XF_E_CANNOT_OPEN_FILE;

        /* process the stream and be sure to close it even on error */
        rc = process_stream(nio, &map[i], &stream);
        rc2 = xf_stream_close(&stream);

        /* bail if either operation raised an error */
        if(rc)
            return rc;
        if(rc2)
            return rc2;
    }

    /* now flush to ensure all data was written */
    rc = xf_nandio_flush(nio);
    if(rc)
        return rc;

    return XF_E_SUCCESS;
}

static const enum xf_nandio_mode update_mode_to_nandio[] = {
    [XF_UPDATE] = XF_NANDIO_PROGRAM,
    [XF_BACKUP] = XF_NANDIO_READ,
    [XF_VERIFY] = XF_NANDIO_VERIFY,
};

int xf_updater_run(enum xf_update_mode mode, struct xf_nandio* nio,
                   struct xf_map* map, int map_size,
                   xf_update_open_stream_cb open_stream, void* arg)
{
    /* Switch NAND I/O into the correct mode */
    int rc = xf_nandio_set_mode(nio, update_mode_to_nandio[mode]);
    if(rc)
        return rc;

    /* This does all the real work */
    return process_map(nio, map, map_size, open_stream, arg);
}