purgatory.c 1.4 KB

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