Wed Jan 1 10:35:10 2020 UTC ()
Adjust image size appropriately when using update_image

OK jmcneill


(skrll)
diff -r1.27 -r1.28 src/usr.bin/mkubootimage/mkubootimage.c

cvs diff -r1.27 -r1.28 src/usr.bin/mkubootimage/mkubootimage.c (expand / switch to unified diff)

--- src/usr.bin/mkubootimage/mkubootimage.c 2019/12/07 12:34:17 1.27
+++ src/usr.bin/mkubootimage/mkubootimage.c 2020/01/01 10:35:10 1.28
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: mkubootimage.c,v 1.27 2019/12/07 12:34:17 wiz Exp $ */ 1/* $NetBSD: mkubootimage.c,v 1.28 2020/01/01 10:35:10 skrll Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca> 4 * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products 12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission. 13 * derived from this software without specific prior written permission.
14 * 14 *
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE. 25 * SUCH DAMAGE.
26 */ 26 */
27 27
28#if HAVE_NBTOOL_CONFIG_H 28#if HAVE_NBTOOL_CONFIG_H
29#include "nbtool_config.h" 29#include "nbtool_config.h"
30#endif 30#endif
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__RCSID("$NetBSD: mkubootimage.c,v 1.27 2019/12/07 12:34:17 wiz Exp $"); 33__RCSID("$NetBSD: mkubootimage.c,v 1.28 2020/01/01 10:35:10 skrll Exp $");
34 34
35#include <sys/mman.h> 35#include <sys/mman.h>
36#include <sys/stat.h> 36#include <sys/stat.h>
37#include <sys/endian.h> 37#include <sys/endian.h>
38#include <sys/param.h> 38#include <sys/param.h>
39#include <sys/uio.h> 39#include <sys/uio.h>
40#include <err.h> 40#include <err.h>
41#include <errno.h> 41#include <errno.h>
42#include <fcntl.h> 42#include <fcntl.h>
43#include <inttypes.h> 43#include <inttypes.h>
44#include <limits.h> 44#include <limits.h>
45#include <stdint.h> 45#include <stdint.h>
46#include <stdio.h> 46#include <stdio.h>
@@ -324,27 +324,28 @@ generate_header_uimg(struct uboot_image_ @@ -324,27 +324,28 @@ generate_header_uimg(struct uboot_image_
324 if (image_type == IH_TYPE_SCRIPT) { 324 if (image_type == IH_TYPE_SCRIPT) {
325 struct iovec iov[3]; 325 struct iovec iov[3];
326 dsize = st.st_size + (sizeof(uint32_t) * 2); 326 dsize = st.st_size + (sizeof(uint32_t) * 2);
327 size_buf[0] = htonl(st.st_size); 327 size_buf[0] = htonl(st.st_size);
328 size_buf[1] = htonl(0); 328 size_buf[1] = htonl(0);
329 iov[0].iov_base = &size_buf[0]; 329 iov[0].iov_base = &size_buf[0];
330 iov[0].iov_len = sizeof(size_buf[0]); 330 iov[0].iov_len = sizeof(size_buf[0]);
331 iov[1].iov_base = &size_buf[1]; 331 iov[1].iov_base = &size_buf[1];
332 iov[1].iov_len = sizeof(size_buf[1]); 332 iov[1].iov_len = sizeof(size_buf[1]);
333 iov[2].iov_base = p; 333 iov[2].iov_base = p;
334 iov[2].iov_len = st.st_size; 334 iov[2].iov_len = st.st_size;
335 crc = crc32v(iov, 3); 335 crc = crc32v(iov, 3);
336 } else { 336 } else {
337 dsize = st.st_size; 337 dsize = update_image ?
 338 st.st_size - sizeof(*hdr) : st.st_size;
338 crc = crc32(p, st.st_size); 339 crc = crc32(p, st.st_size);
339 } 340 }
340 munmap(p, st.st_size); 341 munmap(p, st.st_size);
341 342
342 memset(hdr, 0, sizeof(*hdr)); 343 memset(hdr, 0, sizeof(*hdr));
343 hdr->ih_magic = htonl(image_magic); 344 hdr->ih_magic = htonl(image_magic);
344 hdr->ih_time = htonl(st.st_mtime); 345 hdr->ih_time = htonl(st.st_mtime);
345 hdr->ih_size = htonl(dsize); 346 hdr->ih_size = htonl(dsize);
346 hdr->ih_load = htonl(image_loadaddr); 347 hdr->ih_load = htonl(image_loadaddr);
347 hdr->ih_ep = htonl(image_entrypoint); 348 hdr->ih_ep = htonl(image_entrypoint);
348 hdr->ih_dcrc = htonl(crc); 349 hdr->ih_dcrc = htonl(crc);
349 hdr->ih_os = image_os; 350 hdr->ih_os = image_os;
350 hdr->ih_arch = image_arch; 351 hdr->ih_arch = image_arch;
@@ -377,30 +378,33 @@ generate_header_arm64(struct arm64_image @@ -377,30 +378,33 @@ generate_header_arm64(struct arm64_image
377 378
378 error = fstat(kernel_fd, &st); 379 error = fstat(kernel_fd, &st);
379 if (error == -1) { 380 if (error == -1) {
380 perror("stat"); 381 perror("stat");
381 return errno; 382 return errno;
382 } 383 }
383 384
384 flags = 0; 385 flags = 0;
385 flags |= ARM64_FLAGS_PAGE_SIZE_4K; 386 flags |= ARM64_FLAGS_PAGE_SIZE_4K;
386#if 0 387#if 0
387 flags |= ARM64_FLAGS_PHYS_PLACEMENT_ANY; 388 flags |= ARM64_FLAGS_PHYS_PLACEMENT_ANY;
388#endif 389#endif
389 390
 391 const uint64_t dsize = update_image ?
 392 st.st_size - sizeof(*hdr) : st.st_size;
 393
390 memset(hdr, 0, sizeof(*hdr)); 394 memset(hdr, 0, sizeof(*hdr));
391 hdr->code0 = htole32(ARM64_CODE0); 395 hdr->code0 = htole32(ARM64_CODE0);
392 hdr->text_offset = htole64(image_entrypoint); 396 hdr->text_offset = htole64(image_entrypoint);
393 hdr->image_size = htole64(st.st_size + sizeof(*hdr)); 397 hdr->image_size = htole64(dsize);
394 hdr->flags = htole32(flags); 398 hdr->flags = htole32(flags);
395 hdr->magic = htole32(ARM64_MAGIC); 399 hdr->magic = htole32(ARM64_MAGIC);
396 400
397 dump_header_arm64(hdr); 401 dump_header_arm64(hdr);
398 402
399 return 0; 403 return 0;
400} 404}
401 405
402static int 406static int
403write_image(void *hdr, size_t hdrlen, int kernel_fd, int image_fd) 407write_image(void *hdr, size_t hdrlen, int kernel_fd, int image_fd)
404{ 408{
405 uint8_t buf[4096]; 409 uint8_t buf[4096];
406 ssize_t rlen, wlen; 410 ssize_t rlen, wlen;