blob: 025c34ac0d848f642a4b4b1c2c9577c52aa72614 [file] [log] [blame]
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -07001/*
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
Thomas Gleixner40c50c12017-03-10 13:17:18 +010013#include <linux/bug.h>
Philipp Rudodf6f2802018-04-13 15:36:46 -070014#include <linux/sha256.h>
Thomas Gleixner40c50c12017-03-10 13:17:18 +010015#include <asm/purgatory.h>
16
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070017#include "../boot/string.h"
18
Thomas Gleixner40c50c12017-03-10 13:17:18 +010019unsigned long purgatory_backup_dest __section(.kexec-purgatory);
20unsigned long purgatory_backup_src __section(.kexec-purgatory);
21unsigned long purgatory_backup_sz __section(.kexec-purgatory);
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070022
Thomas Gleixner40c50c12017-03-10 13:17:18 +010023u8 purgatory_sha256_digest[SHA256_DIGEST_SIZE] __section(.kexec-purgatory);
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070024
Thomas Gleixner40c50c12017-03-10 13:17:18 +010025struct kexec_sha_region purgatory_sha_regions[KEXEC_SEGMENT_MAX] __section(.kexec-purgatory);
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070026
27/*
28 * On x86, second kernel requries first 640K of memory to boot. Copy
29 * first 640K to a backup region in reserved memory range so that second
30 * kernel can use first 640K.
31 */
32static int copy_backup_region(void)
33{
Thomas Gleixner40c50c12017-03-10 13:17:18 +010034 if (purgatory_backup_dest) {
35 memcpy((void *)purgatory_backup_dest,
36 (void *)purgatory_backup_src, purgatory_backup_sz);
37 }
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070038 return 0;
39}
40
Tobin C. Harding72042a82017-02-20 10:12:35 +110041static int verify_sha256_digest(void)
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070042{
Thomas Gleixner40c50c12017-03-10 13:17:18 +010043 struct kexec_sha_region *ptr, *end;
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070044 u8 digest[SHA256_DIGEST_SIZE];
45 struct sha256_state sctx;
46
47 sha256_init(&sctx);
Thomas Gleixner40c50c12017-03-10 13:17:18 +010048 end = purgatory_sha_regions + ARRAY_SIZE(purgatory_sha_regions);
49
50 for (ptr = purgatory_sha_regions; ptr < end; ptr++)
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070051 sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
52
53 sha256_final(&sctx, digest);
54
Thomas Gleixner40c50c12017-03-10 13:17:18 +010055 if (memcmp(digest, purgatory_sha256_digest, sizeof(digest)))
Vivek Goyal8fc5b4d2014-08-08 14:26:02 -070056 return 1;
57
58 return 0;
59}
60
61void purgatory(void)
62{
63 int ret;
64
65 ret = verify_sha256_digest();
66 if (ret) {
67 /* loop forever */
68 for (;;)
69 ;
70 }
71 copy_backup_region();
72}