purgatory.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * purgatory: Runs between two kernels
  3. *
  4. * Copyright (C) 2014 Red Hat Inc.
  5. *
  6. * Author:
  7. * Vivek Goyal <vgoyal@redhat.com>
  8. *
  9. * This source code is licensed under the GNU General Public License,
  10. * Version 2. See the file COPYING for more details.
  11. */
  12. #include <linux/bug.h>
  13. #include <linux/sha256.h>
  14. #include <asm/purgatory.h>
  15. #include "../boot/string.h"
  16. unsigned long purgatory_backup_dest __section(.kexec-purgatory);
  17. unsigned long purgatory_backup_src __section(.kexec-purgatory);
  18. unsigned long purgatory_backup_sz __section(.kexec-purgatory);
  19. u8 purgatory_sha256_digest[SHA256_DIGEST_SIZE] __section(.kexec-purgatory);
  20. struct kexec_sha_region purgatory_sha_regions[KEXEC_SEGMENT_MAX] __section(.kexec-purgatory);
  21. /*
  22. * On x86, second kernel requries first 640K of memory to boot. Copy
  23. * first 640K to a backup region in reserved memory range so that second
  24. * kernel can use first 640K.
  25. */
  26. static int copy_backup_region(void)
  27. {
  28. if (purgatory_backup_dest) {
  29. memcpy((void *)purgatory_backup_dest,
  30. (void *)purgatory_backup_src, purgatory_backup_sz);
  31. }
  32. return 0;
  33. }
  34. static int verify_sha256_digest(void)
  35. {
  36. struct kexec_sha_region *ptr, *end;
  37. u8 digest[SHA256_DIGEST_SIZE];
  38. struct sha256_state sctx;
  39. sha256_init(&sctx);
  40. end = purgatory_sha_regions + ARRAY_SIZE(purgatory_sha_regions);
  41. for (ptr = purgatory_sha_regions; ptr < end; ptr++)
  42. sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
  43. sha256_final(&sctx, digest);
  44. if (memcmp(digest, purgatory_sha256_digest, sizeof(digest)))
  45. return 1;
  46. return 0;
  47. }
  48. void purgatory(void)
  49. {
  50. int ret;
  51. ret = verify_sha256_digest();
  52. if (ret) {
  53. /* loop forever */
  54. for (;;)
  55. ;
  56. }
  57. copy_backup_region();
  58. }
  59. /*
  60. * Defined in order to reuse memcpy() and memset() from
  61. * arch/x86/boot/compressed/string.c
  62. */
  63. void warn(const char *msg) {}