purgatory.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "sha256.h"
  13. #include "purgatory.h"
  14. #include "../boot/string.h"
  15. struct sha_region {
  16. unsigned long start;
  17. unsigned long len;
  18. };
  19. static unsigned long backup_dest;
  20. static unsigned long backup_src;
  21. static unsigned long backup_sz;
  22. static u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
  23. struct sha_region sha_regions[16] = {};
  24. /*
  25. * On x86, second kernel requries first 640K of memory to boot. Copy
  26. * first 640K to a backup region in reserved memory range so that second
  27. * kernel can use first 640K.
  28. */
  29. static int copy_backup_region(void)
  30. {
  31. if (backup_dest)
  32. memcpy((void *)backup_dest, (void *)backup_src, backup_sz);
  33. return 0;
  34. }
  35. static int verify_sha256_digest(void)
  36. {
  37. struct sha_region *ptr, *end;
  38. u8 digest[SHA256_DIGEST_SIZE];
  39. struct sha256_state sctx;
  40. sha256_init(&sctx);
  41. end = &sha_regions[sizeof(sha_regions)/sizeof(sha_regions[0])];
  42. for (ptr = sha_regions; ptr < end; ptr++)
  43. sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
  44. sha256_final(&sctx, digest);
  45. if (memcmp(digest, sha256_digest, sizeof(digest)))
  46. return 1;
  47. return 0;
  48. }
  49. void purgatory(void)
  50. {
  51. int ret;
  52. ret = verify_sha256_digest();
  53. if (ret) {
  54. /* loop forever */
  55. for (;;)
  56. ;
  57. }
  58. copy_backup_region();
  59. }