-
Notifications
You must be signed in to change notification settings - Fork 8
/
sunxi-spl.c
142 lines (116 loc) · 3.13 KB
/
sunxi-spl.c
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
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2018, Andre Przywara
*
* sunxi-spl: dump information about a mainline U-Boot SPL, wrapped into
* an Allwinner eGON header
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "sunxi-fw.h"
#define CHECKSUM_SEED 0x5f0a6c39
struct spl_boot_file_head {
uint32_t jump_instruction;
uint8_t magic[8];
uint32_t check_sum;
uint32_t length;
uint8_t spl_signature[4];
uint32_t fel_script_address;
uint32_t fel_uEnv_length;
uint32_t offset_dt_name;
uint32_t reserved1;
uint32_t boot_media;
uint32_t string_pool[13];
};
/* Like strstr(), but does not stop at \0. */
static const char *memstr(const char *haystack, const char *needle, size_t size)
{
size_t i;
for (i = 0; i <= size - strlen(needle); i++) {
if (haystack[i] != needle[0])
continue;
if (!memcmp(haystack + i, needle, strlen(needle)))
return haystack + i;
}
return NULL;
}
int output_spl_info(void *sector, FILE *inf, FILE *stream, bool verbose)
{
struct spl_boot_file_head *splhead = sector;
uint32_t *buffer, i, chksum = CHECKSUM_SEED;
const char *spl_banner;
uint32_t length;
size_t ret;
if (splhead->spl_signature[3] >= 2 &&
splhead->offset_dt_name != 0 &&
splhead->offset_dt_name < 512)
fprintf(stream, "\tDT: %s\n",
(char *)splhead + splhead->offset_dt_name);
length = splhead->length > 32768 ? splhead->length : 32768;
if (!verbose) {
pseek(inf, length - 512);
return 63;
}
fprintf(stream, "\tsize: %d bytes\n", splhead->length);
buffer = malloc(length);
if (!buffer)
return 0;
ret = fread(buffer + (512 / 4), 1, length - 512, inf);
if (ret < splhead->length - 512) {
fprintf(stream, "\tERROR: image file too small\n");
free(buffer);
return ret / 512;
}
memcpy(buffer, sector, 512);
for (i = 0; i < splhead->length / 4; i++)
if (i != 3)
chksum += buffer[i];
if (chksum == splhead->check_sum)
fprintf(stream, "\teGON checksum matches: 0x%08x\n", chksum);
else
fprintf(stream, "\teGON checksum: 0x%08x, programmed: 0x%08x\n",
chksum, splhead->check_sum);
spl_banner = memstr((char *)buffer, "U-Boot SPL ", length);
if (spl_banner)
fprintf(stream, "\t%s\n", spl_banner);
free(buffer);
return ret / 512;
}
int handle_dt_name(FILE *inf, const char *dt_name, FILE *outf)
{
struct spl_boot_file_head *splhead;
char sector[512];
size_t ret;
enum image_type type;
ret = fread(sector, 1, 512, inf);
if (ret < 512) {
fprintf(stderr, "Cannot read from input file\n");
return -3;
}
type = identify_image(sector);
if (type == IMAGE_MBR) {
pseek(inf, 8192 - 512);
ret = fread(sector, 1, 512, inf);
if (ret < 512) {
fprintf(stderr, "Cannot read from input file\n");
return -3;
}
type = identify_image(sector);
}
if (type != IMAGE_SPL2) {
fprintf(stderr, "expecting U-Boot SPLv2\n");
return -4;
}
splhead = (void *)sector;
if (splhead->spl_signature[3] < 2 ||
splhead->offset_dt_name == 0 ||
splhead->offset_dt_name >= 512) {
fprintf(stderr, "no DT name found.\n");
return -5;
}
fprintf(outf, "%s\n", sector + splhead->offset_dt_name);
return 0;
}