mkpiggy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (fread(&olen, sizeof(olen), 1, f) != 1) {
  56. perror(argv[1]);
  57. return 1;
  58. }
  59. ilen = ftell(f);
  60. olen = getle32(&olen);
  61. fclose(f);
  62. /*
  63. * Now we have the input (compressed) and output (uncompressed)
  64. * sizes, compute the necessary decompression offset...
  65. */
  66. offs = (olen > ilen) ? olen - ilen : 0;
  67. offs += olen >> 12; /* Add 8 bytes for each 32K block */
  68. offs += 64*1024 + 128; /* Add 64K + 128 bytes slack */
  69. offs = (offs+4095) & ~4095; /* Round to a 4K boundary */
  70. printf(".section \".rodata..compressed\",\"a\",@progbits\n");
  71. printf(".globl z_input_len\n");
  72. printf("z_input_len = %lu\n", ilen);
  73. printf(".globl z_output_len\n");
  74. printf("z_output_len = %lu\n", (unsigned long)olen);
  75. printf(".globl z_extract_offset\n");
  76. printf("z_extract_offset = 0x%lx\n", offs);
  77. /* z_extract_offset_negative allows simplification of head_32.S */
  78. printf(".globl z_extract_offset_negative\n");
  79. printf("z_extract_offset_negative = -0x%lx\n", offs);
  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. return 0;
  85. }