mkpiggy.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. static uint32_t getle32(const void *p)
  31. {
  32. const uint8_t *cp = p;
  33. return (uint32_t)cp[0] + ((uint32_t)cp[1] << 8) +
  34. ((uint32_t)cp[2] << 16) + ((uint32_t)cp[3] << 24);
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. uint32_t olen;
  39. long ilen;
  40. unsigned long offs;
  41. FILE *f;
  42. if (argc < 2) {
  43. fprintf(stderr, "Usage: %s compressed_file\n", argv[0]);
  44. return 1;
  45. }
  46. /* Get the information for the compressed kernel image first */
  47. f = fopen(argv[1], "r");
  48. if (!f) {
  49. perror(argv[1]);
  50. return 1;
  51. }
  52. if (fseek(f, -4L, SEEK_END)) {
  53. perror(argv[1]);
  54. }
  55. fread(&olen, sizeof olen, 1, f);
  56. ilen = ftell(f);
  57. olen = getle32(&olen);
  58. fclose(f);
  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 += 32*1024 + 18; /* Add 32K + 18 bytes slack */
  66. offs = (offs+4095) & ~4095; /* Round to a 4K boundary */
  67. printf(".section \".rodata.compressed\",\"a\",@progbits\n");
  68. printf(".globl z_input_len\n");
  69. printf("z_input_len = %lu\n", ilen);
  70. printf(".globl z_output_len\n");
  71. printf("z_output_len = %lu\n", (unsigned long)olen);
  72. printf(".globl z_extract_offset\n");
  73. printf("z_extract_offset = 0x%lx\n", offs);
  74. /* z_extract_offset_negative allows simplification of head_32.S */
  75. printf(".globl z_extract_offset_negative\n");
  76. printf("z_extract_offset_negative = -0x%lx\n", offs);
  77. printf(".globl input_data, input_data_end\n");
  78. printf("input_data:\n");
  79. printf(".incbin \"%s\"\n", argv[1]);
  80. printf("input_data_end:\n");
  81. return 0;
  82. }