efi_variable.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * UEFI runtime variable services
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <env.h>
  10. #include <env_internal.h>
  11. #include <hexdump.h>
  12. #include <malloc.h>
  13. #include <rtc.h>
  14. #include <search.h>
  15. #include <uuid.h>
  16. #include <crypto/pkcs7_parser.h>
  17. #include <linux/bitops.h>
  18. #include <linux/compat.h>
  19. #include <u-boot/crc.h>
  20. enum efi_secure_mode {
  21. EFI_MODE_SETUP,
  22. EFI_MODE_USER,
  23. EFI_MODE_AUDIT,
  24. EFI_MODE_DEPLOYED,
  25. };
  26. static bool efi_secure_boot;
  27. static enum efi_secure_mode efi_secure_mode;
  28. static u8 efi_vendor_keys;
  29. #define READ_ONLY BIT(31)
  30. static efi_status_t efi_get_variable_common(u16 *variable_name,
  31. const efi_guid_t *vendor,
  32. u32 *attributes,
  33. efi_uintn_t *data_size, void *data,
  34. u64 *timep);
  35. static efi_status_t efi_set_variable_common(u16 *variable_name,
  36. const efi_guid_t *vendor,
  37. u32 attributes,
  38. efi_uintn_t data_size,
  39. const void *data,
  40. bool ro_check);
  41. /*
  42. * Mapping between EFI variables and u-boot variables:
  43. *
  44. * efi_$guid_$varname = {attributes}(type)value
  45. *
  46. * For example:
  47. *
  48. * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
  49. * "{ro,boot,run}(blob)0000000000000000"
  50. * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
  51. * "(blob)00010000"
  52. *
  53. * The attributes are a comma separated list of these possible
  54. * attributes:
  55. *
  56. * + ro - read-only
  57. * + boot - boot-services access
  58. * + run - runtime access
  59. *
  60. * NOTE: with current implementation, no variables are available after
  61. * ExitBootServices, and all are persisted (if possible).
  62. *
  63. * If not specified, the attributes default to "{boot}".
  64. *
  65. * The required type is one of:
  66. *
  67. * + utf8 - raw utf8 string
  68. * + blob - arbitrary length hex string
  69. *
  70. * Maybe a utf16 type would be useful to for a string value to be auto
  71. * converted to utf16?
  72. */
  73. #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
  74. /**
  75. * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
  76. * variable name
  77. *
  78. * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
  79. * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
  80. * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
  81. *
  82. * @native: pointer to pointer to U-Boot variable name
  83. * @variable_name: UEFI variable name
  84. * @vendor: vendor GUID
  85. * Return: status code
  86. */
  87. static efi_status_t efi_to_native(char **native, const u16 *variable_name,
  88. const efi_guid_t *vendor)
  89. {
  90. size_t len;
  91. char *pos;
  92. len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
  93. *native = malloc(len);
  94. if (!*native)
  95. return EFI_OUT_OF_RESOURCES;
  96. pos = *native;
  97. pos += sprintf(pos, "efi_%pUl_", vendor);
  98. utf16_utf8_strcpy(&pos, variable_name);
  99. return EFI_SUCCESS;
  100. }
  101. /**
  102. * prefix() - skip over prefix
  103. *
  104. * Skip over a prefix string.
  105. *
  106. * @str: string with prefix
  107. * @prefix: prefix string
  108. * Return: string without prefix, or NULL if prefix not found
  109. */
  110. static const char *prefix(const char *str, const char *prefix)
  111. {
  112. size_t n = strlen(prefix);
  113. if (!strncmp(prefix, str, n))
  114. return str + n;
  115. return NULL;
  116. }
  117. /**
  118. * parse_attr() - decode attributes part of variable value
  119. *
  120. * Convert the string encoded attributes of a UEFI variable to a bit mask.
  121. * TODO: Several attributes are not supported.
  122. *
  123. * @str: value of U-Boot variable
  124. * @attrp: pointer to UEFI attributes
  125. * @timep: pointer to time attribute
  126. * Return: pointer to remainder of U-Boot variable value
  127. */
  128. static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
  129. {
  130. u32 attr = 0;
  131. char sep = '{';
  132. if (*str != '{') {
  133. *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
  134. return str;
  135. }
  136. while (*str == sep) {
  137. const char *s;
  138. str++;
  139. if ((s = prefix(str, "ro"))) {
  140. attr |= READ_ONLY;
  141. } else if ((s = prefix(str, "nv"))) {
  142. attr |= EFI_VARIABLE_NON_VOLATILE;
  143. } else if ((s = prefix(str, "boot"))) {
  144. attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  145. } else if ((s = prefix(str, "run"))) {
  146. attr |= EFI_VARIABLE_RUNTIME_ACCESS;
  147. } else if ((s = prefix(str, "time="))) {
  148. attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  149. hex2bin((u8 *)timep, s, sizeof(*timep));
  150. s += sizeof(*timep) * 2;
  151. } else if (*str == '}') {
  152. break;
  153. } else {
  154. printf("invalid attribute: %s\n", str);
  155. break;
  156. }
  157. str = s;
  158. sep = ',';
  159. }
  160. str++;
  161. *attrp = attr;
  162. return str;
  163. }
  164. /**
  165. * efi_set_secure_state - modify secure boot state variables
  166. * @secure_boot: value of SecureBoot
  167. * @setup_mode: value of SetupMode
  168. * @audit_mode: value of AuditMode
  169. * @deployed_mode: value of DeployedMode
  170. *
  171. * Modify secure boot status related variables as indicated.
  172. *
  173. * Return: status code
  174. */
  175. static efi_status_t efi_set_secure_state(u8 secure_boot, u8 setup_mode,
  176. u8 audit_mode, u8 deployed_mode)
  177. {
  178. u32 attributes;
  179. efi_status_t ret;
  180. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  181. EFI_VARIABLE_RUNTIME_ACCESS |
  182. READ_ONLY;
  183. ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
  184. attributes, sizeof(secure_boot),
  185. &secure_boot, false);
  186. if (ret != EFI_SUCCESS)
  187. goto err;
  188. ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
  189. attributes, sizeof(setup_mode),
  190. &setup_mode, false);
  191. if (ret != EFI_SUCCESS)
  192. goto err;
  193. ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
  194. attributes, sizeof(audit_mode),
  195. &audit_mode, false);
  196. if (ret != EFI_SUCCESS)
  197. goto err;
  198. ret = efi_set_variable_common(L"DeployedMode",
  199. &efi_global_variable_guid, attributes,
  200. sizeof(deployed_mode), &deployed_mode,
  201. false);
  202. err:
  203. return ret;
  204. }
  205. /**
  206. * efi_transfer_secure_state - handle a secure boot state transition
  207. * @mode: new state
  208. *
  209. * Depending on @mode, secure boot related variables are updated.
  210. * Those variables are *read-only* for users, efi_set_variable_common()
  211. * is called here.
  212. *
  213. * Return: status code
  214. */
  215. static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
  216. {
  217. efi_status_t ret;
  218. EFI_PRINT("Switching secure state from %d to %d\n", efi_secure_mode,
  219. mode);
  220. if (mode == EFI_MODE_DEPLOYED) {
  221. ret = efi_set_secure_state(1, 0, 0, 1);
  222. if (ret != EFI_SUCCESS)
  223. goto err;
  224. efi_secure_boot = true;
  225. } else if (mode == EFI_MODE_AUDIT) {
  226. ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
  227. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  228. EFI_VARIABLE_RUNTIME_ACCESS,
  229. 0, NULL, false);
  230. if (ret != EFI_SUCCESS)
  231. goto err;
  232. ret = efi_set_secure_state(0, 1, 1, 0);
  233. if (ret != EFI_SUCCESS)
  234. goto err;
  235. efi_secure_boot = true;
  236. } else if (mode == EFI_MODE_USER) {
  237. ret = efi_set_secure_state(1, 0, 0, 0);
  238. if (ret != EFI_SUCCESS)
  239. goto err;
  240. efi_secure_boot = true;
  241. } else if (mode == EFI_MODE_SETUP) {
  242. ret = efi_set_secure_state(0, 1, 0, 0);
  243. if (ret != EFI_SUCCESS)
  244. goto err;
  245. } else {
  246. return EFI_INVALID_PARAMETER;
  247. }
  248. efi_secure_mode = mode;
  249. return EFI_SUCCESS;
  250. err:
  251. /* TODO: What action should be taken here? */
  252. printf("ERROR: Secure state transition failed\n");
  253. return ret;
  254. }
  255. /**
  256. * efi_init_secure_state - initialize secure boot state
  257. *
  258. * Return: status code
  259. */
  260. static efi_status_t efi_init_secure_state(void)
  261. {
  262. enum efi_secure_mode mode;
  263. efi_uintn_t size;
  264. efi_status_t ret;
  265. /*
  266. * TODO:
  267. * Since there is currently no "platform-specific" installation
  268. * method of Platform Key, we can't say if VendorKeys is 0 or 1
  269. * precisely.
  270. */
  271. size = 0;
  272. ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
  273. NULL, &size, NULL, NULL);
  274. if (ret == EFI_BUFFER_TOO_SMALL) {
  275. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
  276. mode = EFI_MODE_USER;
  277. else
  278. mode = EFI_MODE_SETUP;
  279. efi_vendor_keys = 0;
  280. } else if (ret == EFI_NOT_FOUND) {
  281. mode = EFI_MODE_SETUP;
  282. efi_vendor_keys = 1;
  283. } else {
  284. goto err;
  285. }
  286. ret = efi_transfer_secure_state(mode);
  287. if (ret == EFI_SUCCESS)
  288. ret = efi_set_variable_common(L"VendorKeys",
  289. &efi_global_variable_guid,
  290. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  291. EFI_VARIABLE_RUNTIME_ACCESS |
  292. READ_ONLY,
  293. sizeof(efi_vendor_keys),
  294. &efi_vendor_keys, false);
  295. err:
  296. return ret;
  297. }
  298. /**
  299. * efi_secure_boot_enabled - return if secure boot is enabled or not
  300. *
  301. * Return: true if enabled, false if disabled
  302. */
  303. bool efi_secure_boot_enabled(void)
  304. {
  305. return efi_secure_boot;
  306. }
  307. #ifdef CONFIG_EFI_SECURE_BOOT
  308. static u8 pkcs7_hdr[] = {
  309. /* SEQUENCE */
  310. 0x30, 0x82, 0x05, 0xc7,
  311. /* OID: pkcs7-signedData */
  312. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
  313. /* Context Structured? */
  314. 0xa0, 0x82, 0x05, 0xb8,
  315. };
  316. /**
  317. * efi_variable_parse_signature - parse a signature in variable
  318. * @buf: Pointer to variable's value
  319. * @buflen: Length of @buf
  320. *
  321. * Parse a signature embedded in variable's value and instantiate
  322. * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
  323. * pkcs7's signedData, some header needed be prepended for correctly
  324. * parsing authentication data, particularly for variable's.
  325. *
  326. * Return: Pointer to pkcs7_message structure on success, NULL on error
  327. */
  328. static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
  329. size_t buflen)
  330. {
  331. u8 *ebuf;
  332. size_t ebuflen, len;
  333. struct pkcs7_message *msg;
  334. /*
  335. * This is the best assumption to check if the binary is
  336. * already in a form of pkcs7's signedData.
  337. */
  338. if (buflen > sizeof(pkcs7_hdr) &&
  339. !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
  340. msg = pkcs7_parse_message(buf, buflen);
  341. goto out;
  342. }
  343. /*
  344. * Otherwise, we should add a dummy prefix sequence for pkcs7
  345. * message parser to be able to process.
  346. * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
  347. * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
  348. * TODO:
  349. * The header should be composed in a more refined manner.
  350. */
  351. EFI_PRINT("Makeshift prefix added to authentication data\n");
  352. ebuflen = sizeof(pkcs7_hdr) + buflen;
  353. if (ebuflen <= 0x7f) {
  354. EFI_PRINT("Data is too short\n");
  355. return NULL;
  356. }
  357. ebuf = malloc(ebuflen);
  358. if (!ebuf) {
  359. EFI_PRINT("Out of memory\n");
  360. return NULL;
  361. }
  362. memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
  363. memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
  364. len = ebuflen - 4;
  365. ebuf[2] = (len >> 8) & 0xff;
  366. ebuf[3] = len & 0xff;
  367. len = ebuflen - 0x13;
  368. ebuf[0x11] = (len >> 8) & 0xff;
  369. ebuf[0x12] = len & 0xff;
  370. msg = pkcs7_parse_message(ebuf, ebuflen);
  371. free(ebuf);
  372. out:
  373. if (IS_ERR(msg))
  374. return NULL;
  375. return msg;
  376. }
  377. /**
  378. * efi_variable_authenticate - authenticate a variable
  379. * @variable: Variable name in u16
  380. * @vendor: Guid of variable
  381. * @data_size: Size of @data
  382. * @data: Pointer to variable's value
  383. * @given_attr: Attributes to be given at SetVariable()
  384. * @env_attr: Attributes that an existing variable holds
  385. * @time: signed time that an existing variable holds
  386. *
  387. * Called by efi_set_variable() to verify that the input is correct.
  388. * Will replace the given data pointer with another that points to
  389. * the actual data to store in the internal memory.
  390. * On success, @data and @data_size will be replaced with variable's
  391. * actual data, excluding authentication data, and its size, and variable's
  392. * attributes and signed time will also be returned in @env_attr and @time,
  393. * respectively.
  394. *
  395. * Return: status code
  396. */
  397. static efi_status_t efi_variable_authenticate(u16 *variable,
  398. const efi_guid_t *vendor,
  399. efi_uintn_t *data_size,
  400. const void **data, u32 given_attr,
  401. u32 *env_attr, u64 *time)
  402. {
  403. const struct efi_variable_authentication_2 *auth;
  404. struct efi_signature_store *truststore, *truststore2;
  405. struct pkcs7_message *var_sig;
  406. struct efi_image_regions *regs;
  407. struct efi_time timestamp;
  408. struct rtc_time tm;
  409. u64 new_time;
  410. efi_status_t ret;
  411. var_sig = NULL;
  412. truststore = NULL;
  413. truststore2 = NULL;
  414. regs = NULL;
  415. ret = EFI_SECURITY_VIOLATION;
  416. if (*data_size < sizeof(struct efi_variable_authentication_2))
  417. goto err;
  418. /* authentication data */
  419. auth = *data;
  420. if (*data_size < (sizeof(auth->time_stamp)
  421. + auth->auth_info.hdr.dwLength))
  422. goto err;
  423. if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
  424. goto err;
  425. memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
  426. if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone ||
  427. timestamp.daylight || timestamp.pad2)
  428. goto err;
  429. *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
  430. *data_size -= (sizeof(auth->time_stamp)
  431. + auth->auth_info.hdr.dwLength);
  432. memset(&tm, 0, sizeof(tm));
  433. tm.tm_year = timestamp.year;
  434. tm.tm_mon = timestamp.month;
  435. tm.tm_mday = timestamp.day;
  436. tm.tm_hour = timestamp.hour;
  437. tm.tm_min = timestamp.minute;
  438. tm.tm_sec = timestamp.second;
  439. new_time = rtc_mktime(&tm);
  440. if (!efi_secure_boot_enabled()) {
  441. /* finished checking */
  442. *time = new_time;
  443. return EFI_SUCCESS;
  444. }
  445. if (new_time <= *time)
  446. goto err;
  447. /* data to be digested */
  448. regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
  449. if (!regs)
  450. goto err;
  451. regs->max = 5;
  452. efi_image_region_add(regs, (uint8_t *)variable,
  453. (uint8_t *)variable
  454. + u16_strlen(variable) * sizeof(u16), 1);
  455. efi_image_region_add(regs, (uint8_t *)vendor,
  456. (uint8_t *)vendor + sizeof(*vendor), 1);
  457. efi_image_region_add(regs, (uint8_t *)&given_attr,
  458. (uint8_t *)&given_attr + sizeof(given_attr), 1);
  459. efi_image_region_add(regs, (uint8_t *)&timestamp,
  460. (uint8_t *)&timestamp + sizeof(timestamp), 1);
  461. efi_image_region_add(regs, (uint8_t *)*data,
  462. (uint8_t *)*data + *data_size, 1);
  463. /* variable's signature list */
  464. if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
  465. goto err;
  466. var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
  467. auth->auth_info.hdr.dwLength
  468. - sizeof(auth->auth_info));
  469. if (!var_sig) {
  470. EFI_PRINT("Parsing variable's signature failed\n");
  471. goto err;
  472. }
  473. /* signature database used for authentication */
  474. if (u16_strcmp(variable, L"PK") == 0 ||
  475. u16_strcmp(variable, L"KEK") == 0) {
  476. /* with PK */
  477. truststore = efi_sigstore_parse_sigdb(L"PK");
  478. if (!truststore)
  479. goto err;
  480. } else if (u16_strcmp(variable, L"db") == 0 ||
  481. u16_strcmp(variable, L"dbx") == 0) {
  482. /* with PK and KEK */
  483. truststore = efi_sigstore_parse_sigdb(L"KEK");
  484. truststore2 = efi_sigstore_parse_sigdb(L"PK");
  485. if (!truststore) {
  486. if (!truststore2)
  487. goto err;
  488. truststore = truststore2;
  489. truststore2 = NULL;
  490. }
  491. } else {
  492. /* TODO: support private authenticated variables */
  493. goto err;
  494. }
  495. /* verify signature */
  496. if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
  497. EFI_PRINT("Verified\n");
  498. } else {
  499. if (truststore2 &&
  500. efi_signature_verify_with_sigdb(regs, var_sig,
  501. truststore2, NULL)) {
  502. EFI_PRINT("Verified\n");
  503. } else {
  504. EFI_PRINT("Verifying variable's signature failed\n");
  505. goto err;
  506. }
  507. }
  508. /* finished checking */
  509. *time = new_time;
  510. ret = EFI_SUCCESS;
  511. err:
  512. efi_sigstore_free(truststore);
  513. efi_sigstore_free(truststore2);
  514. pkcs7_free_message(var_sig);
  515. free(regs);
  516. return ret;
  517. }
  518. #else
  519. static efi_status_t efi_variable_authenticate(u16 *variable,
  520. const efi_guid_t *vendor,
  521. efi_uintn_t *data_size,
  522. const void **data, u32 given_attr,
  523. u32 *env_attr, u64 *time)
  524. {
  525. return EFI_SUCCESS;
  526. }
  527. #endif /* CONFIG_EFI_SECURE_BOOT */
  528. static efi_status_t efi_get_variable_common(u16 *variable_name,
  529. const efi_guid_t *vendor,
  530. u32 *attributes,
  531. efi_uintn_t *data_size, void *data,
  532. u64 *timep)
  533. {
  534. char *native_name;
  535. efi_status_t ret;
  536. unsigned long in_size;
  537. const char *val = NULL, *s;
  538. u64 time = 0;
  539. u32 attr;
  540. if (!variable_name || !vendor || !data_size)
  541. return EFI_INVALID_PARAMETER;
  542. ret = efi_to_native(&native_name, variable_name, vendor);
  543. if (ret)
  544. return ret;
  545. EFI_PRINT("get '%s'\n", native_name);
  546. val = env_get(native_name);
  547. free(native_name);
  548. if (!val)
  549. return EFI_NOT_FOUND;
  550. val = parse_attr(val, &attr, &time);
  551. if (timep)
  552. *timep = time;
  553. in_size = *data_size;
  554. if ((s = prefix(val, "(blob)"))) {
  555. size_t len = strlen(s);
  556. /* number of hexadecimal digits must be even */
  557. if (len & 1)
  558. return EFI_DEVICE_ERROR;
  559. /* two characters per byte: */
  560. len /= 2;
  561. *data_size = len;
  562. if (in_size < len) {
  563. ret = EFI_BUFFER_TOO_SMALL;
  564. goto out;
  565. }
  566. if (!data) {
  567. EFI_PRINT("Variable with no data shouldn't exist.\n");
  568. return EFI_INVALID_PARAMETER;
  569. }
  570. if (hex2bin(data, s, len))
  571. return EFI_DEVICE_ERROR;
  572. EFI_PRINT("got value: \"%s\"\n", s);
  573. } else if ((s = prefix(val, "(utf8)"))) {
  574. unsigned len = strlen(s) + 1;
  575. *data_size = len;
  576. if (in_size < len) {
  577. ret = EFI_BUFFER_TOO_SMALL;
  578. goto out;
  579. }
  580. if (!data) {
  581. EFI_PRINT("Variable with no data shouldn't exist.\n");
  582. return EFI_INVALID_PARAMETER;
  583. }
  584. memcpy(data, s, len);
  585. ((char *)data)[len] = '\0';
  586. EFI_PRINT("got value: \"%s\"\n", (char *)data);
  587. } else {
  588. EFI_PRINT("invalid value: '%s'\n", val);
  589. return EFI_DEVICE_ERROR;
  590. }
  591. out:
  592. if (attributes)
  593. *attributes = attr & EFI_VARIABLE_MASK;
  594. return ret;
  595. }
  596. /**
  597. * efi_efi_get_variable() - retrieve value of a UEFI variable
  598. *
  599. * This function implements the GetVariable runtime service.
  600. *
  601. * See the Unified Extensible Firmware Interface (UEFI) specification for
  602. * details.
  603. *
  604. * @variable_name: name of the variable
  605. * @vendor: vendor GUID
  606. * @attributes: attributes of the variable
  607. * @data_size: size of the buffer to which the variable value is copied
  608. * @data: buffer to which the variable value is copied
  609. * Return: status code
  610. */
  611. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  612. const efi_guid_t *vendor, u32 *attributes,
  613. efi_uintn_t *data_size, void *data)
  614. {
  615. efi_status_t ret;
  616. EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
  617. data_size, data);
  618. ret = efi_get_variable_common(variable_name, vendor, attributes,
  619. data_size, data, NULL);
  620. return EFI_EXIT(ret);
  621. }
  622. static char *efi_variables_list;
  623. static char *efi_cur_variable;
  624. /**
  625. * parse_uboot_variable() - parse a u-boot variable and get uefi-related
  626. * information
  627. * @variable: whole data of u-boot variable (ie. name=value)
  628. * @variable_name_size: size of variable_name buffer in byte
  629. * @variable_name: name of uefi variable in u16, null-terminated
  630. * @vendor: vendor's guid
  631. * @attributes: attributes
  632. *
  633. * A uefi variable is encoded into a u-boot variable as described above.
  634. * This function parses such a u-boot variable and retrieve uefi-related
  635. * information into respective parameters. In return, variable_name_size
  636. * is the size of variable name including NULL.
  637. *
  638. * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
  639. * the entire variable list has been returned,
  640. * otherwise non-zero status code
  641. */
  642. static efi_status_t parse_uboot_variable(char *variable,
  643. efi_uintn_t *variable_name_size,
  644. u16 *variable_name,
  645. const efi_guid_t *vendor,
  646. u32 *attributes)
  647. {
  648. char *guid, *name, *end, c;
  649. size_t name_len;
  650. efi_uintn_t old_variable_name_size;
  651. u64 time;
  652. u16 *p;
  653. guid = strchr(variable, '_');
  654. if (!guid)
  655. return EFI_INVALID_PARAMETER;
  656. guid++;
  657. name = strchr(guid, '_');
  658. if (!name)
  659. return EFI_INVALID_PARAMETER;
  660. name++;
  661. end = strchr(name, '=');
  662. if (!end)
  663. return EFI_INVALID_PARAMETER;
  664. name_len = end - name;
  665. old_variable_name_size = *variable_name_size;
  666. *variable_name_size = sizeof(u16) * (name_len + 1);
  667. if (old_variable_name_size < *variable_name_size)
  668. return EFI_BUFFER_TOO_SMALL;
  669. end++; /* point to value */
  670. /* variable name */
  671. p = variable_name;
  672. utf8_utf16_strncpy(&p, name, name_len);
  673. variable_name[name_len] = 0;
  674. /* guid */
  675. c = *(name - 1);
  676. *(name - 1) = '\0'; /* guid need be null-terminated here */
  677. if (uuid_str_to_bin(guid, (unsigned char *)vendor,
  678. UUID_STR_FORMAT_GUID))
  679. /* The only error would be EINVAL. */
  680. return EFI_INVALID_PARAMETER;
  681. *(name - 1) = c;
  682. /* attributes */
  683. parse_attr(end, attributes, &time);
  684. return EFI_SUCCESS;
  685. }
  686. /**
  687. * efi_get_next_variable_name() - enumerate the current variable names
  688. *
  689. * @variable_name_size: size of variable_name buffer in byte
  690. * @variable_name: name of uefi variable's name in u16
  691. * @vendor: vendor's guid
  692. *
  693. * This function implements the GetNextVariableName service.
  694. *
  695. * See the Unified Extensible Firmware Interface (UEFI) specification for
  696. * details.
  697. *
  698. * Return: status code
  699. */
  700. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  701. u16 *variable_name,
  702. efi_guid_t *vendor)
  703. {
  704. char *native_name, *variable;
  705. ssize_t name_len, list_len;
  706. char regex[256];
  707. char * const regexlist[] = {regex};
  708. u32 attributes;
  709. int i;
  710. efi_status_t ret;
  711. EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
  712. if (!variable_name_size || !variable_name || !vendor)
  713. return EFI_EXIT(EFI_INVALID_PARAMETER);
  714. if (variable_name[0]) {
  715. /* check null-terminated string */
  716. for (i = 0; i < *variable_name_size; i++)
  717. if (!variable_name[i])
  718. break;
  719. if (i >= *variable_name_size)
  720. return EFI_EXIT(EFI_INVALID_PARAMETER);
  721. /* search for the last-returned variable */
  722. ret = efi_to_native(&native_name, variable_name, vendor);
  723. if (ret)
  724. return EFI_EXIT(ret);
  725. name_len = strlen(native_name);
  726. for (variable = efi_variables_list; variable && *variable;) {
  727. if (!strncmp(variable, native_name, name_len) &&
  728. variable[name_len] == '=')
  729. break;
  730. variable = strchr(variable, '\n');
  731. if (variable)
  732. variable++;
  733. }
  734. free(native_name);
  735. if (!(variable && *variable))
  736. return EFI_EXIT(EFI_INVALID_PARAMETER);
  737. /* next variable */
  738. variable = strchr(variable, '\n');
  739. if (variable)
  740. variable++;
  741. if (!(variable && *variable))
  742. return EFI_EXIT(EFI_NOT_FOUND);
  743. } else {
  744. /*
  745. *new search: free a list used in the previous search
  746. */
  747. free(efi_variables_list);
  748. efi_variables_list = NULL;
  749. efi_cur_variable = NULL;
  750. snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
  751. list_len = hexport_r(&env_htab, '\n',
  752. H_MATCH_REGEX | H_MATCH_KEY,
  753. &efi_variables_list, 0, 1, regexlist);
  754. if (list_len <= 1)
  755. return EFI_EXIT(EFI_NOT_FOUND);
  756. variable = efi_variables_list;
  757. }
  758. ret = parse_uboot_variable(variable, variable_name_size, variable_name,
  759. vendor, &attributes);
  760. return EFI_EXIT(ret);
  761. }
  762. static efi_status_t efi_set_variable_common(u16 *variable_name,
  763. const efi_guid_t *vendor,
  764. u32 attributes,
  765. efi_uintn_t data_size,
  766. const void *data,
  767. bool ro_check)
  768. {
  769. char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
  770. efi_uintn_t old_size;
  771. bool append, delete;
  772. u64 time = 0;
  773. u32 attr;
  774. efi_status_t ret = EFI_SUCCESS;
  775. if (!variable_name || !*variable_name || !vendor ||
  776. ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
  777. !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
  778. ret = EFI_INVALID_PARAMETER;
  779. goto err;
  780. }
  781. ret = efi_to_native(&native_name, variable_name, vendor);
  782. if (ret)
  783. goto err;
  784. /* check if a variable exists */
  785. old_size = 0;
  786. attr = 0;
  787. ret = efi_get_variable_common(variable_name, vendor, &attr,
  788. &old_size, NULL, &time);
  789. append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
  790. attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
  791. delete = !append && (!data_size || !attributes);
  792. /* check attributes */
  793. if (old_size) {
  794. if (ro_check && (attr & READ_ONLY)) {
  795. ret = EFI_WRITE_PROTECTED;
  796. goto err;
  797. }
  798. /* attributes won't be changed */
  799. if (!delete &&
  800. ((ro_check && attr != attributes) ||
  801. (!ro_check && ((attr & ~(u32)READ_ONLY)
  802. != (attributes & ~(u32)READ_ONLY))))) {
  803. ret = EFI_INVALID_PARAMETER;
  804. goto err;
  805. }
  806. } else {
  807. if (delete || append) {
  808. /*
  809. * Trying to delete or to update a non-existent
  810. * variable.
  811. */
  812. ret = EFI_NOT_FOUND;
  813. goto err;
  814. }
  815. }
  816. if (((!u16_strcmp(variable_name, L"PK") ||
  817. !u16_strcmp(variable_name, L"KEK")) &&
  818. !guidcmp(vendor, &efi_global_variable_guid)) ||
  819. ((!u16_strcmp(variable_name, L"db") ||
  820. !u16_strcmp(variable_name, L"dbx")) &&
  821. !guidcmp(vendor, &efi_guid_image_security_database))) {
  822. /* authentication is mandatory */
  823. if (!(attributes &
  824. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
  825. EFI_PRINT("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
  826. variable_name);
  827. ret = EFI_INVALID_PARAMETER;
  828. goto err;
  829. }
  830. }
  831. /* authenticate a variable */
  832. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
  833. if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
  834. ret = EFI_INVALID_PARAMETER;
  835. goto err;
  836. }
  837. if (attributes &
  838. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  839. ret = efi_variable_authenticate(variable_name, vendor,
  840. &data_size, &data,
  841. attributes, &attr,
  842. &time);
  843. if (ret != EFI_SUCCESS)
  844. goto err;
  845. /* last chance to check for delete */
  846. if (!data_size)
  847. delete = true;
  848. }
  849. } else {
  850. if (attributes &
  851. (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
  852. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
  853. EFI_PRINT("Secure boot is not configured\n");
  854. ret = EFI_INVALID_PARAMETER;
  855. goto err;
  856. }
  857. }
  858. /* delete a variable */
  859. if (delete) {
  860. /* !old_size case has been handled before */
  861. val = NULL;
  862. ret = EFI_SUCCESS;
  863. goto out;
  864. }
  865. if (append) {
  866. old_data = malloc(old_size);
  867. if (!old_data) {
  868. ret = EFI_OUT_OF_RESOURCES;
  869. goto err;
  870. }
  871. ret = efi_get_variable_common(variable_name, vendor,
  872. &attr, &old_size, old_data, NULL);
  873. if (ret != EFI_SUCCESS)
  874. goto err;
  875. } else {
  876. old_size = 0;
  877. }
  878. val = malloc(2 * old_size + 2 * data_size
  879. + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
  880. + 1);
  881. if (!val) {
  882. ret = EFI_OUT_OF_RESOURCES;
  883. goto err;
  884. }
  885. s = val;
  886. /*
  887. * store attributes
  888. */
  889. attributes &= (READ_ONLY |
  890. EFI_VARIABLE_NON_VOLATILE |
  891. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  892. EFI_VARIABLE_RUNTIME_ACCESS |
  893. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
  894. s += sprintf(s, "{");
  895. while (attributes) {
  896. attr = 1 << (ffs(attributes) - 1);
  897. if (attr == READ_ONLY) {
  898. s += sprintf(s, "ro");
  899. } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
  900. s += sprintf(s, "nv");
  901. } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
  902. s += sprintf(s, "boot");
  903. } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
  904. s += sprintf(s, "run");
  905. } else if (attr ==
  906. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  907. s += sprintf(s, "time=");
  908. s = bin2hex(s, (u8 *)&time, sizeof(time));
  909. }
  910. attributes &= ~attr;
  911. if (attributes)
  912. s += sprintf(s, ",");
  913. }
  914. s += sprintf(s, "}");
  915. s += sprintf(s, "(blob)");
  916. /* store payload: */
  917. if (append)
  918. s = bin2hex(s, old_data, old_size);
  919. s = bin2hex(s, data, data_size);
  920. *s = '\0';
  921. EFI_PRINT("setting: %s=%s\n", native_name, val);
  922. out:
  923. if (env_set(native_name, val)) {
  924. ret = EFI_DEVICE_ERROR;
  925. } else {
  926. bool vendor_keys_modified = false;
  927. if ((u16_strcmp(variable_name, L"PK") == 0 &&
  928. guidcmp(vendor, &efi_global_variable_guid) == 0)) {
  929. ret = efi_transfer_secure_state(
  930. (delete ? EFI_MODE_SETUP :
  931. EFI_MODE_USER));
  932. if (ret != EFI_SUCCESS)
  933. goto err;
  934. if (efi_secure_mode != EFI_MODE_SETUP)
  935. vendor_keys_modified = true;
  936. } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
  937. guidcmp(vendor, &efi_global_variable_guid) == 0)) {
  938. if (efi_secure_mode != EFI_MODE_SETUP)
  939. vendor_keys_modified = true;
  940. }
  941. /* update VendorKeys */
  942. if (vendor_keys_modified & efi_vendor_keys) {
  943. efi_vendor_keys = 0;
  944. ret = efi_set_variable_common(
  945. L"VendorKeys",
  946. &efi_global_variable_guid,
  947. EFI_VARIABLE_BOOTSERVICE_ACCESS
  948. | EFI_VARIABLE_RUNTIME_ACCESS
  949. | READ_ONLY,
  950. sizeof(efi_vendor_keys),
  951. &efi_vendor_keys,
  952. false);
  953. } else {
  954. ret = EFI_SUCCESS;
  955. }
  956. }
  957. err:
  958. free(native_name);
  959. free(old_data);
  960. free(val);
  961. return ret;
  962. }
  963. /**
  964. * efi_set_variable() - set value of a UEFI variable
  965. *
  966. * This function implements the SetVariable runtime service.
  967. *
  968. * See the Unified Extensible Firmware Interface (UEFI) specification for
  969. * details.
  970. *
  971. * @variable_name: name of the variable
  972. * @vendor: vendor GUID
  973. * @attributes: attributes of the variable
  974. * @data_size: size of the buffer with the variable value
  975. * @data: buffer with the variable value
  976. * Return: status code
  977. */
  978. efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
  979. const efi_guid_t *vendor, u32 attributes,
  980. efi_uintn_t data_size, const void *data)
  981. {
  982. EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
  983. data_size, data);
  984. /* READ_ONLY bit is not part of API */
  985. attributes &= ~(u32)READ_ONLY;
  986. return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
  987. attributes, data_size, data,
  988. true));
  989. }
  990. /**
  991. * efi_query_variable_info() - get information about EFI variables
  992. *
  993. * This function implements the QueryVariableInfo() runtime service.
  994. *
  995. * See the Unified Extensible Firmware Interface (UEFI) specification for
  996. * details.
  997. *
  998. * @attributes: bitmask to select variables to be
  999. * queried
  1000. * @maximum_variable_storage_size: maximum size of storage area for the
  1001. * selected variable types
  1002. * @remaining_variable_storage_size: remaining size of storage are for the
  1003. * selected variable types
  1004. * @maximum_variable_size: maximum size of a variable of the
  1005. * selected type
  1006. * Returns: status code
  1007. */
  1008. efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
  1009. u32 attributes,
  1010. u64 *maximum_variable_storage_size,
  1011. u64 *remaining_variable_storage_size,
  1012. u64 *maximum_variable_size)
  1013. {
  1014. return EFI_UNSUPPORTED;
  1015. }
  1016. /**
  1017. * efi_get_variable_runtime() - runtime implementation of GetVariable()
  1018. *
  1019. * @variable_name: name of the variable
  1020. * @vendor: vendor GUID
  1021. * @attributes: attributes of the variable
  1022. * @data_size: size of the buffer to which the variable value is copied
  1023. * @data: buffer to which the variable value is copied
  1024. * Return: status code
  1025. */
  1026. static efi_status_t __efi_runtime EFIAPI
  1027. efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
  1028. u32 *attributes, efi_uintn_t *data_size, void *data)
  1029. {
  1030. return EFI_UNSUPPORTED;
  1031. }
  1032. /**
  1033. * efi_get_next_variable_name_runtime() - runtime implementation of
  1034. * GetNextVariable()
  1035. *
  1036. * @variable_name_size: size of variable_name buffer in byte
  1037. * @variable_name: name of uefi variable's name in u16
  1038. * @vendor: vendor's guid
  1039. * Return: status code
  1040. */
  1041. static efi_status_t __efi_runtime EFIAPI
  1042. efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
  1043. u16 *variable_name, efi_guid_t *vendor)
  1044. {
  1045. return EFI_UNSUPPORTED;
  1046. }
  1047. /**
  1048. * efi_set_variable_runtime() - runtime implementation of SetVariable()
  1049. *
  1050. * @variable_name: name of the variable
  1051. * @vendor: vendor GUID
  1052. * @attributes: attributes of the variable
  1053. * @data_size: size of the buffer with the variable value
  1054. * @data: buffer with the variable value
  1055. * Return: status code
  1056. */
  1057. static efi_status_t __efi_runtime EFIAPI
  1058. efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
  1059. u32 attributes, efi_uintn_t data_size,
  1060. const void *data)
  1061. {
  1062. return EFI_UNSUPPORTED;
  1063. }
  1064. /**
  1065. * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
  1066. */
  1067. void efi_variables_boot_exit_notify(void)
  1068. {
  1069. efi_runtime_services.get_variable = efi_get_variable_runtime;
  1070. efi_runtime_services.get_next_variable_name =
  1071. efi_get_next_variable_name_runtime;
  1072. efi_runtime_services.set_variable = efi_set_variable_runtime;
  1073. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  1074. }
  1075. /**
  1076. * efi_init_variables() - initialize variable services
  1077. *
  1078. * Return: status code
  1079. */
  1080. efi_status_t efi_init_variables(void)
  1081. {
  1082. efi_status_t ret;
  1083. ret = efi_init_secure_state();
  1084. return ret;
  1085. }