Linux 4.9.243
[linux/fpc-iii.git] / arch / x86 / purgatory / purgatory.c
blob25e068ba338214826413265b26ccd9179e10b7f4
1 /*
2 * purgatory: Runs between two kernels
4 * Copyright (C) 2014 Red Hat Inc.
6 * Author:
7 * Vivek Goyal <vgoyal@redhat.com>
9 * This source code is licensed under the GNU General Public License,
10 * Version 2. See the file COPYING for more details.
13 #include "sha256.h"
14 #include "../boot/string.h"
16 struct sha_region {
17 unsigned long start;
18 unsigned long len;
21 unsigned long backup_dest = 0;
22 unsigned long backup_src = 0;
23 unsigned long backup_sz = 0;
25 u8 sha256_digest[SHA256_DIGEST_SIZE] = { 0 };
27 struct sha_region sha_regions[16] = {};
30 * On x86, second kernel requries first 640K of memory to boot. Copy
31 * first 640K to a backup region in reserved memory range so that second
32 * kernel can use first 640K.
34 static int copy_backup_region(void)
36 if (backup_dest)
37 memcpy((void *)backup_dest, (void *)backup_src, backup_sz);
39 return 0;
42 int verify_sha256_digest(void)
44 struct sha_region *ptr, *end;
45 u8 digest[SHA256_DIGEST_SIZE];
46 struct sha256_state sctx;
48 sha256_init(&sctx);
49 end = &sha_regions[sizeof(sha_regions)/sizeof(sha_regions[0])];
50 for (ptr = sha_regions; ptr < end; ptr++)
51 sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
53 sha256_final(&sctx, digest);
55 if (memcmp(digest, sha256_digest, sizeof(digest)))
56 return 1;
58 return 0;
61 void purgatory(void)
63 int ret;
65 ret = verify_sha256_digest();
66 if (ret) {
67 /* loop forever */
68 for (;;)
71 copy_backup_region();