efi_variable.c 28 KB

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