mkpiggy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright (C) 2009 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version
  7. * 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * H. Peter Anvin <hpa@linux.intel.com>
  20. *
  21. * ----------------------------------------------------------------------- */
  22. /*
  23. * Compute the desired load offset from a compressed program; outputs
  24. * a small assembly wrapper with the appropriate symbols defined.
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <inttypes.h>
  30. #include <tools/le_byteshift.h>
  31. int main(int argc, char *argv[])
  32. {
  33. uint32_t olen;
  34. long ilen;
  35. unsigned long offs;
  36. unsigned long run_size;
  37. FILE *f = NULL;
  38. int retval = 1;
  39. if (argc < 3) {
  40. fprintf(stderr, "Usage: %s compressed_file run_size\n",
  41. argv[0]);
  42. goto bail;
  43. }
  44. /* Get the information for the compressed kernel image first */
  45. f = fopen(argv[1], "r");
  46. if (!f) {
  47. perror(argv[1]);
  48. goto bail;
  49. }
  50. if (fseek(f, -4L, SEEK_END)) {
  51. perror(argv[1]);
  52. }
  53. if (fread(&olen, sizeof(olen), 1, f) != 1) {
  54. perror(argv[1]);
  55. goto bail;
  56. }
  57. ilen = ftell(f);
  58. olen = get_unaligned_le32(&olen);
  59. /*
  60. * Now we have the input (compressed) and output (uncompressed)
  61. * sizes, compute the necessary decompression offset...
  62. */
  63. offs = (olen > ilen) ? olen - ilen : 0;
  64. offs += olen >> 12; /* Add 8 bytes for each 32K block */
  65. offs += 64*1024 + 128; /* Add 64K + 128 bytes slack */
  66. offs = (offs+4095) & ~4095; /* Round to a 4K boundary */
  67. run_size = atoi(argv[2]);
  68. printf(".section \".rodata..compressed\",\"a\",@progbits\n");
  69. printf(".globl z_input_len\n");
  70. printf("z_input_len = %lu\n", ilen);
  71. printf(".globl z_output_len\n");
  72. printf("z_output_len = %lu\n", (unsigned long)olen);
  73. printf(".globl z_extract_offset\n");
  74. printf("z_extract_offset = 0x%lx\n", offs);
  75. /* z_extract_offset_negative allows simplification of head_32.S */
  76. printf(".globl z_extract_offset_negative\n");
  77. printf("z_extract_offset_negative = -0x%lx\n", offs);
  78. printf(".globl z_run_size\n");
  79. printf("z_run_size = %lu\n", run_size);
  80. printf(".globl input_data, input_data_end\n");
  81. printf("input_data:\n");
  82. printf(".incbin \"%s\"\n", argv[1]);
  83. printf("input_data_end:\n");
  84. retval = 0;
  85. bail:
  86. if (f)
  87. fclose(f);
  88. return retval;
  89. }