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
|
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <image.h>
#include <getopt.h>
#include <arch/options.h>
#include "kexec.h"
#include <kexec-uImage.h>
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
/*
* Basic uImage loader. Not rocket science.
*/
/*
* Returns the image type if everything goes well. This would
* allow the user to decide if the image is of their interest.
*
* Returns -1 on a corrupted image
*
* Returns 0 if this is not a uImage
*/
int uImage_probe(const char *buf, off_t len, unsigned int arch)
{
struct image_header header;
#ifdef HAVE_LIBZ
unsigned int crc;
unsigned int hcrc;
#endif
if ((uintmax_t)len < (uintmax_t)sizeof(header))
return -1;
memcpy(&header, buf, sizeof(header));
if (be32_to_cpu(header.ih_magic) != IH_MAGIC)
return 0;
#ifdef HAVE_LIBZ
hcrc = be32_to_cpu(header.ih_hcrc);
header.ih_hcrc = 0;
crc = crc32(0, (void *)&header, sizeof(header));
if (crc != hcrc) {
printf("Header checksum of the uImage does not match\n");
return -1;
}
#endif
switch (header.ih_type) {
case IH_TYPE_KERNEL:
case IH_TYPE_KERNEL_NOLOAD:
break;
case IH_TYPE_RAMDISK:
break;
default:
printf("uImage type %d unsupported\n", header.ih_type);
return -1;
}
if (header.ih_os != IH_OS_LINUX) {
printf("uImage os %d unsupported\n", header.ih_os);
return -1;
}
if (header.ih_arch != arch) {
printf("uImage arch %d unsupported\n", header.ih_arch);
return -1;
}
switch (header.ih_comp) {
case IH_COMP_NONE:
#ifdef HAVE_LIBZ
case IH_COMP_GZIP:
#endif
break;
default:
printf("uImage uses unsupported compression method\n");
return -1;
}
if (be32_to_cpu(header.ih_size) > len - sizeof(header)) {
printf("uImage header claims that image has %d bytes\n",
be32_to_cpu(header.ih_size));
printf("we read only %lld bytes.\n",
(long long)len - sizeof(header));
return -1;
}
#ifdef HAVE_LIBZ
crc = crc32(0, (void *)buf + sizeof(header), be32_to_cpu(header.ih_size));
if (crc != be32_to_cpu(header.ih_dcrc)) {
printf("uImage: The data CRC does not match. Computed: %08x "
"expected %08x\n", crc,
be32_to_cpu(header.ih_dcrc));
return -1;
}
#endif
return (int)header.ih_type;
}
/*
* To conform to the 'probe' routine in file_type struct,
* we return :
* 0 - If the image is valid 'type' image.
*
* Now, we have to pass on the 'errors' in the image. So,
*
* -1 - If the image is corrupted.
* 1 - If the image is not a uImage.
*/
int uImage_probe_kernel(const char *buf, off_t len, unsigned int arch)
{
int type = uImage_probe(buf, len, arch);
if (type < 0)
return -1;
return !(type == IH_TYPE_KERNEL || type == IH_TYPE_KERNEL_NOLOAD);
}
int uImage_probe_ramdisk(const char *buf, off_t len, unsigned int arch)
{
int type = uImage_probe(buf, len, arch);
if (type < 0)
return -1;
return !(type == IH_TYPE_RAMDISK);
}
#ifdef HAVE_LIBZ
/* gzip flag byte */
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
#define COMMENT 0x10 /* bit 4 set: file comment present */
#define RESERVED 0xE0 /* bits 5..7: reserved */
static int uImage_gz_load(const char *buf, off_t len,
struct Image_info *image)
{
int ret;
z_stream strm;
unsigned int skip;
unsigned int flags;
unsigned char *uncomp_buf;
unsigned int mem_alloc;
mem_alloc = 10 * 1024 * 1024;
uncomp_buf = malloc(mem_alloc);
if (!uncomp_buf)
return -1;
memset(&strm, 0, sizeof(strm));
/* Skip magic, method, time, flags, os code ... */
skip = 10;
/* check GZ magic */
if (buf[0] != 0x1f || buf[1] != 0x8b) {
free(uncomp_buf);
return -1;
}
flags = buf[3];
if (buf[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
puts ("Error: Bad gzipped data\n");
free(uncomp_buf);
return -1;
}
if (flags & EXTRA_FIELD) {
skip += 2;
skip += buf[10];
skip += buf[11] << 8;
}
if (flags & ORIG_NAME) {
while (buf[skip++])
;
}
if (flags & COMMENT) {
while (buf[skip++])
;
}
if (flags & HEAD_CRC)
skip += 2;
strm.avail_in = len - skip;
strm.next_in = (void *)buf + skip;
/* - activates parsing gz headers */
ret = inflateInit2(&strm, -MAX_WBITS);
if (ret != Z_OK) {
free(uncomp_buf);
return -1;
}
strm.next_out = uncomp_buf;
strm.avail_out = mem_alloc;
do {
ret = inflate(&strm, Z_FINISH);
if (ret == Z_STREAM_END)
break;
if (ret == Z_OK || ret == Z_BUF_ERROR) {
void *new_buf;
int inc_buf = 5 * 1024 * 1024;
mem_alloc += inc_buf;
new_buf = realloc(uncomp_buf, mem_alloc);
if (!new_buf) {
inflateEnd(&strm);
free(uncomp_buf);
return -1;
}
uncomp_buf = new_buf;
strm.next_out = uncomp_buf + mem_alloc - inc_buf;
strm.avail_out = inc_buf;
} else {
free(uncomp_buf);
printf("Error during decompression %d\n", ret);
return -1;
}
} while (1);
inflateEnd(&strm);
image->buf = (char *)uncomp_buf;
image->len = mem_alloc - strm.avail_out;
return 0;
}
#else
static int uImage_gz_load(const char *UNUSED(buf), off_t UNUSED(len),
struct Image_info *UNUSED(image))
{
return -1;
}
#endif
int uImage_load(const char *buf, off_t len, struct Image_info *image)
{
const struct image_header *header = (const struct image_header *)buf;
const char *img_buf = buf + sizeof(struct image_header);
off_t img_len = be32_to_cpu(header->ih_size);
/*
* Prevent loading a modified image.
* CRC check is perfomed only when zlib is compiled
* in. This check will help us to detect
* size related vulnerabilities.
*/
if (img_len != (len - sizeof(struct image_header))) {
printf("Image size doesn't match the header\n");
return -1;
}
image->base = cpu_to_be32(header->ih_load);
image->ep = cpu_to_be32(header->ih_ep);
switch (header->ih_comp) {
case IH_COMP_NONE:
image->buf = img_buf;
image->len = img_len;
return 0;
break;
case IH_COMP_GZIP:
/*
* uboot doesn't decompress the RAMDISK images.
* Comply to the uboot behaviour.
*/
if (header->ih_type == IH_TYPE_RAMDISK) {
image->buf = img_buf;
image->len = img_len;
return 0;
} else
return uImage_gz_load(img_buf, img_len, image);
break;
default:
return -1;
}
}
|