calc_vmlinuz_load_addr.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2010 "Wu Zhangjin" <wuzhangjin@gmail.com>
  4. */
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <linux/sizes.h>
  12. int main(int argc, char *argv[])
  13. {
  14. unsigned long long vmlinux_size, vmlinux_load_addr, vmlinuz_load_addr;
  15. struct stat sb;
  16. if (argc != 3) {
  17. fprintf(stderr, "Usage: %s <pathname> <vmlinux_load_addr>\n",
  18. argv[0]);
  19. return EXIT_FAILURE;
  20. }
  21. if (stat(argv[1], &sb) == -1) {
  22. perror("stat");
  23. return EXIT_FAILURE;
  24. }
  25. /* Convert hex characters to dec number */
  26. errno = 0;
  27. if (sscanf(argv[2], "%llx", &vmlinux_load_addr) != 1) {
  28. if (errno != 0)
  29. perror("sscanf");
  30. else
  31. fprintf(stderr, "No matching characters\n");
  32. return EXIT_FAILURE;
  33. }
  34. vmlinux_size = (uint64_t)sb.st_size;
  35. vmlinuz_load_addr = vmlinux_load_addr + vmlinux_size;
  36. /*
  37. * Align with 64KB: KEXEC needs load sections to be aligned to PAGE_SIZE,
  38. * which may be as large as 64KB depending on the kernel configuration.
  39. */
  40. vmlinuz_load_addr += (SZ_64K - vmlinux_size % SZ_64K);
  41. printf("0x%llx\n", vmlinuz_load_addr);
  42. return EXIT_SUCCESS;
  43. }