14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0+
24febfb8dSArd Biesheuvel
304851772SMatt Fleming #include <linux/efi.h>
404851772SMatt Fleming #include <linux/module.h>
504851772SMatt Fleming #include <linux/pstore.h>
620b4fb48SLinus Torvalds #include <linux/slab.h>
7a614e192SMatt Fleming #include <linux/ucs2_string.h>
804851772SMatt Fleming
985974825SArd Biesheuvel MODULE_IMPORT_NS(EFIVAR);
1085974825SArd Biesheuvel
11efb74e4bSKees Cook #define DUMP_NAME_LEN 66
1204851772SMatt Fleming
13*36d5786aSGuilherme G. Piccoli static unsigned int record_size = 1024;
14*36d5786aSGuilherme G. Piccoli module_param(record_size, uint, 0444);
15*36d5786aSGuilherme G. Piccoli MODULE_PARM_DESC(record_size, "size of each pstore UEFI var (in bytes, min/default=1024)");
16232f4eb6SArd Biesheuvel
1704851772SMatt Fleming static bool efivars_pstore_disable =
1804851772SMatt Fleming IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
1904851772SMatt Fleming
2004851772SMatt Fleming module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
2104851772SMatt Fleming
2204851772SMatt Fleming #define PSTORE_EFI_ATTRIBUTES \
2304851772SMatt Fleming (EFI_VARIABLE_NON_VOLATILE | \
2404851772SMatt Fleming EFI_VARIABLE_BOOTSERVICE_ACCESS | \
2504851772SMatt Fleming EFI_VARIABLE_RUNTIME_ACCESS)
2604851772SMatt Fleming
efi_pstore_open(struct pstore_info * psi)2704851772SMatt Fleming static int efi_pstore_open(struct pstore_info *psi)
2804851772SMatt Fleming {
2985974825SArd Biesheuvel int err;
3085974825SArd Biesheuvel
3185974825SArd Biesheuvel err = efivar_lock();
3285974825SArd Biesheuvel if (err)
3385974825SArd Biesheuvel return err;
3485974825SArd Biesheuvel
35*36d5786aSGuilherme G. Piccoli psi->data = kzalloc(record_size, GFP_KERNEL);
3685974825SArd Biesheuvel if (!psi->data)
3785974825SArd Biesheuvel return -ENOMEM;
3885974825SArd Biesheuvel
3904851772SMatt Fleming return 0;
4004851772SMatt Fleming }
4104851772SMatt Fleming
efi_pstore_close(struct pstore_info * psi)4204851772SMatt Fleming static int efi_pstore_close(struct pstore_info *psi)
4304851772SMatt Fleming {
4485974825SArd Biesheuvel efivar_unlock();
4585974825SArd Biesheuvel kfree(psi->data);
4604851772SMatt Fleming return 0;
4704851772SMatt Fleming }
4804851772SMatt Fleming
generic_id(u64 timestamp,unsigned int part,int count)497aaa822eSKees Cook static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
50fdeadb43SMadper Xie {
517aaa822eSKees Cook return (timestamp * 100 + part) * 1000 + count;
52fdeadb43SMadper Xie }
53fdeadb43SMadper Xie
efi_pstore_read_func(struct pstore_record * record,efi_char16_t * varname)5485974825SArd Biesheuvel static int efi_pstore_read_func(struct pstore_record *record,
5585974825SArd Biesheuvel efi_char16_t *varname)
5604851772SMatt Fleming {
57*36d5786aSGuilherme G. Piccoli unsigned long wlen, size = record_size;
58f8c62f34SAruna Balakrishnaiah char name[DUMP_NAME_LEN], data_type;
5985974825SArd Biesheuvel efi_status_t status;
6004851772SMatt Fleming int cnt;
6104851772SMatt Fleming unsigned int part;
627aaa822eSKees Cook u64 time;
6304851772SMatt Fleming
6485974825SArd Biesheuvel ucs2_as_utf8(name, varname, DUMP_NAME_LEN);
6504851772SMatt Fleming
667aaa822eSKees Cook if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
67125cc42bSKees Cook &record->type, &part, &cnt, &time, &data_type) == 5) {
68125cc42bSKees Cook record->id = generic_id(time, part, cnt);
69c10e8031SKees Cook record->part = part;
70125cc42bSKees Cook record->count = cnt;
71125cc42bSKees Cook record->time.tv_sec = time;
72125cc42bSKees Cook record->time.tv_nsec = 0;
73f8c62f34SAruna Balakrishnaiah if (data_type == 'C')
74125cc42bSKees Cook record->compressed = true;
75f8c62f34SAruna Balakrishnaiah else
76125cc42bSKees Cook record->compressed = false;
77125cc42bSKees Cook record->ecc_notice_size = 0;
787aaa822eSKees Cook } else if (sscanf(name, "dump-type%u-%u-%d-%llu",
79125cc42bSKees Cook &record->type, &part, &cnt, &time) == 4) {
80125cc42bSKees Cook record->id = generic_id(time, part, cnt);
81c10e8031SKees Cook record->part = part;
82125cc42bSKees Cook record->count = cnt;
83125cc42bSKees Cook record->time.tv_sec = time;
84125cc42bSKees Cook record->time.tv_nsec = 0;
85125cc42bSKees Cook record->compressed = false;
86125cc42bSKees Cook record->ecc_notice_size = 0;
877aaa822eSKees Cook } else if (sscanf(name, "dump-type%u-%u-%llu",
88125cc42bSKees Cook &record->type, &part, &time) == 3) {
8904851772SMatt Fleming /*
9004851772SMatt Fleming * Check if an old format,
9104851772SMatt Fleming * which doesn't support holding
9204851772SMatt Fleming * multiple logs, remains.
9304851772SMatt Fleming */
94125cc42bSKees Cook record->id = generic_id(time, part, 0);
95c10e8031SKees Cook record->part = part;
96125cc42bSKees Cook record->count = 0;
97125cc42bSKees Cook record->time.tv_sec = time;
98125cc42bSKees Cook record->time.tv_nsec = 0;
99125cc42bSKees Cook record->compressed = false;
100125cc42bSKees Cook record->ecc_notice_size = 0;
10104851772SMatt Fleming } else
10204851772SMatt Fleming return 0;
10304851772SMatt Fleming
10485974825SArd Biesheuvel record->buf = kmalloc(size, GFP_KERNEL);
105125cc42bSKees Cook if (!record->buf)
106e0d59733SSeiji Aguchi return -ENOMEM;
107e0d59733SSeiji Aguchi
10885974825SArd Biesheuvel status = efivar_get_variable(varname, &LINUX_EFI_CRASH_GUID, NULL,
10985974825SArd Biesheuvel &size, record->buf);
11085974825SArd Biesheuvel if (status != EFI_SUCCESS) {
111125cc42bSKees Cook kfree(record->buf);
11285974825SArd Biesheuvel return -EIO;
113125cc42bSKees Cook }
11485974825SArd Biesheuvel
11585974825SArd Biesheuvel /*
11685974825SArd Biesheuvel * Store the name of the variable in the pstore_record priv field, so
11785974825SArd Biesheuvel * we can reuse it later if we need to delete the EFI variable from the
11885974825SArd Biesheuvel * variable store.
11985974825SArd Biesheuvel */
12085974825SArd Biesheuvel wlen = (ucs2_strnlen(varname, DUMP_NAME_LEN) + 1) * sizeof(efi_char16_t);
12185974825SArd Biesheuvel record->priv = kmemdup(varname, wlen, GFP_KERNEL);
12285974825SArd Biesheuvel if (!record->priv) {
12385974825SArd Biesheuvel kfree(record->buf);
12485974825SArd Biesheuvel return -ENOMEM;
12585974825SArd Biesheuvel }
12685974825SArd Biesheuvel
127e0d59733SSeiji Aguchi return size;
12804851772SMatt Fleming }
12904851772SMatt Fleming
efi_pstore_read(struct pstore_record * record)13085974825SArd Biesheuvel static ssize_t efi_pstore_read(struct pstore_record *record)
13185974825SArd Biesheuvel {
13285974825SArd Biesheuvel efi_char16_t *varname = record->psi->data;
13385974825SArd Biesheuvel efi_guid_t guid = LINUX_EFI_CRASH_GUID;
13485974825SArd Biesheuvel unsigned long varname_size;
13585974825SArd Biesheuvel efi_status_t status;
13685974825SArd Biesheuvel
13785974825SArd Biesheuvel for (;;) {
138*36d5786aSGuilherme G. Piccoli varname_size = 1024;
13985974825SArd Biesheuvel
14085974825SArd Biesheuvel /*
14185974825SArd Biesheuvel * If this is the first read() call in the pstore enumeration,
14285974825SArd Biesheuvel * varname will be the empty string, and the GetNextVariable()
14385974825SArd Biesheuvel * runtime service call will return the first EFI variable in
14485974825SArd Biesheuvel * its own enumeration order, ignoring the guid argument.
14585974825SArd Biesheuvel *
14685974825SArd Biesheuvel * Subsequent calls to GetNextVariable() must pass the name and
14785974825SArd Biesheuvel * guid values returned by the previous call, which is why we
14885974825SArd Biesheuvel * store varname in record->psi->data. Given that we only
14985974825SArd Biesheuvel * enumerate variables with the efi-pstore GUID, there is no
15085974825SArd Biesheuvel * need to record the guid return value.
15185974825SArd Biesheuvel */
15285974825SArd Biesheuvel status = efivar_get_next_variable(&varname_size, varname, &guid);
15385974825SArd Biesheuvel if (status == EFI_NOT_FOUND)
15485974825SArd Biesheuvel return 0;
15585974825SArd Biesheuvel
15685974825SArd Biesheuvel if (status != EFI_SUCCESS)
15785974825SArd Biesheuvel return -EIO;
15885974825SArd Biesheuvel
15985974825SArd Biesheuvel /* skip variables that don't concern us */
16085974825SArd Biesheuvel if (efi_guidcmp(guid, LINUX_EFI_CRASH_GUID))
16185974825SArd Biesheuvel continue;
16285974825SArd Biesheuvel
16385974825SArd Biesheuvel return efi_pstore_read_func(record, varname);
16485974825SArd Biesheuvel }
16585974825SArd Biesheuvel }
16685974825SArd Biesheuvel
efi_pstore_write(struct pstore_record * record)16776cc9580SKees Cook static int efi_pstore_write(struct pstore_record *record)
16804851772SMatt Fleming {
16904851772SMatt Fleming char name[DUMP_NAME_LEN];
17004851772SMatt Fleming efi_char16_t efi_name[DUMP_NAME_LEN];
17185974825SArd Biesheuvel efi_status_t status;
17285974825SArd Biesheuvel int i;
17304851772SMatt Fleming
174c10e8031SKees Cook record->id = generic_id(record->time.tv_sec, record->part,
175c10e8031SKees Cook record->count);
176c10e8031SKees Cook
177efb74e4bSKees Cook /* Since we copy the entire length of name, make sure it is wiped. */
178efb74e4bSKees Cook memset(name, 0, sizeof(name));
179efb74e4bSKees Cook
1807aaa822eSKees Cook snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
18176cc9580SKees Cook record->type, record->part, record->count,
1827aaa822eSKees Cook (long long)record->time.tv_sec,
1837aaa822eSKees Cook record->compressed ? 'C' : 'D');
18404851772SMatt Fleming
18504851772SMatt Fleming for (i = 0; i < DUMP_NAME_LEN; i++)
18604851772SMatt Fleming efi_name[i] = name[i];
18704851772SMatt Fleming
18885974825SArd Biesheuvel if (efivar_trylock())
18985974825SArd Biesheuvel return -EBUSY;
19085974825SArd Biesheuvel status = efivar_set_variable_locked(efi_name, &LINUX_EFI_CRASH_GUID,
19185974825SArd Biesheuvel PSTORE_EFI_ATTRIBUTES,
19285974825SArd Biesheuvel record->size, record->psi->buf,
19385974825SArd Biesheuvel true);
19485974825SArd Biesheuvel efivar_unlock();
19585974825SArd Biesheuvel return status == EFI_SUCCESS ? 0 : -EIO;
19604851772SMatt Fleming };
19704851772SMatt Fleming
efi_pstore_erase(struct pstore_record * record)198efb74e4bSKees Cook static int efi_pstore_erase(struct pstore_record *record)
199efb74e4bSKees Cook {
20085974825SArd Biesheuvel efi_status_t status;
201efb74e4bSKees Cook
20285974825SArd Biesheuvel status = efivar_set_variable(record->priv, &LINUX_EFI_CRASH_GUID,
20385974825SArd Biesheuvel PSTORE_EFI_ATTRIBUTES, 0, NULL);
20404851772SMatt Fleming
20585974825SArd Biesheuvel if (status != EFI_SUCCESS && status != EFI_NOT_FOUND)
20685974825SArd Biesheuvel return -EIO;
20785974825SArd Biesheuvel return 0;
20804851772SMatt Fleming }
20904851772SMatt Fleming
21004851772SMatt Fleming static struct pstore_info efi_pstore_info = {
21104851772SMatt Fleming .owner = THIS_MODULE,
212893c5f1dSGuilherme G. Piccoli .name = KBUILD_MODNAME,
213c950fd6fSNamhyung Kim .flags = PSTORE_FLAGS_DMESG,
21404851772SMatt Fleming .open = efi_pstore_open,
21504851772SMatt Fleming .close = efi_pstore_close,
21604851772SMatt Fleming .read = efi_pstore_read,
21704851772SMatt Fleming .write = efi_pstore_write,
21804851772SMatt Fleming .erase = efi_pstore_erase,
21904851772SMatt Fleming };
22004851772SMatt Fleming
efivars_pstore_init(void)22104851772SMatt Fleming static __init int efivars_pstore_init(void)
22204851772SMatt Fleming {
22385974825SArd Biesheuvel if (!efivar_supports_writes())
22404851772SMatt Fleming return 0;
22504851772SMatt Fleming
22604851772SMatt Fleming if (efivars_pstore_disable)
22704851772SMatt Fleming return 0;
22804851772SMatt Fleming
229*36d5786aSGuilherme G. Piccoli /*
230*36d5786aSGuilherme G. Piccoli * Notice that 1024 is the minimum here to prevent issues with
231*36d5786aSGuilherme G. Piccoli * decompression algorithms that were spotted during tests;
232*36d5786aSGuilherme G. Piccoli * even in the case of not using compression, smaller values would
233*36d5786aSGuilherme G. Piccoli * just pollute more the pstore FS with many small collected files.
234*36d5786aSGuilherme G. Piccoli */
235*36d5786aSGuilherme G. Piccoli if (record_size < 1024)
236*36d5786aSGuilherme G. Piccoli record_size = 1024;
237*36d5786aSGuilherme G. Piccoli
238*36d5786aSGuilherme G. Piccoli efi_pstore_info.buf = kmalloc(record_size, GFP_KERNEL);
23904851772SMatt Fleming if (!efi_pstore_info.buf)
24004851772SMatt Fleming return -ENOMEM;
24104851772SMatt Fleming
242*36d5786aSGuilherme G. Piccoli efi_pstore_info.bufsize = record_size;
24304851772SMatt Fleming
2440d838347SLenny Szubowicz if (pstore_register(&efi_pstore_info)) {
2450d838347SLenny Szubowicz kfree(efi_pstore_info.buf);
2460d838347SLenny Szubowicz efi_pstore_info.buf = NULL;
2470d838347SLenny Szubowicz efi_pstore_info.bufsize = 0;
2480d838347SLenny Szubowicz }
24904851772SMatt Fleming
25004851772SMatt Fleming return 0;
25104851772SMatt Fleming }
25204851772SMatt Fleming
efivars_pstore_exit(void)25304851772SMatt Fleming static __exit void efivars_pstore_exit(void)
25404851772SMatt Fleming {
255cae73167SGeliang Tang if (!efi_pstore_info.bufsize)
256cae73167SGeliang Tang return;
257cae73167SGeliang Tang
258cae73167SGeliang Tang pstore_unregister(&efi_pstore_info);
259cae73167SGeliang Tang kfree(efi_pstore_info.buf);
260cae73167SGeliang Tang efi_pstore_info.buf = NULL;
261cae73167SGeliang Tang efi_pstore_info.bufsize = 0;
26204851772SMatt Fleming }
26304851772SMatt Fleming
26404851772SMatt Fleming module_init(efivars_pstore_init);
26504851772SMatt Fleming module_exit(efivars_pstore_exit);
26604851772SMatt Fleming
26704851772SMatt Fleming MODULE_DESCRIPTION("EFI variable backend for pstore");
26804851772SMatt Fleming MODULE_LICENSE("GPL");
2699ac4d5abSBen Hutchings MODULE_ALIAS("platform:efivars");
270