efivars.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290
  1. /*
  2. * EFI Variables - efivars.c
  3. *
  4. * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
  5. * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
  6. *
  7. * This code takes all variables accessible from EFI runtime and
  8. * exports them via sysfs
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Changelog:
  25. *
  26. * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com>
  27. * remove check for efi_enabled in exit
  28. * add MODULE_VERSION
  29. *
  30. * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com>
  31. * minor bug fixes
  32. *
  33. * 21 Apr 2004 - Matt Tolentino <matthew.e.tolentino@intel.com)
  34. * converted driver to export variable information via sysfs
  35. * and moved to drivers/firmware directory
  36. * bumped revision number to v0.07 to reflect conversion & move
  37. *
  38. * 10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com>
  39. * fix locking per Peter Chubb's findings
  40. *
  41. * 25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com>
  42. * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse()
  43. *
  44. * 12 Feb 2002 - Matt Domsch <Matt_Domsch@dell.com>
  45. * use list_for_each_safe when deleting vars.
  46. * remove ifdef CONFIG_SMP around include <linux/smp.h>
  47. * v0.04 release to linux-ia64@linuxia64.org
  48. *
  49. * 20 April 2001 - Matt Domsch <Matt_Domsch@dell.com>
  50. * Moved vars from /proc/efi to /proc/efi/vars, and made
  51. * efi.c own the /proc/efi directory.
  52. * v0.03 release to linux-ia64@linuxia64.org
  53. *
  54. * 26 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
  55. * At the request of Stephane, moved ownership of /proc/efi
  56. * to efi.c, and now efivars lives under /proc/efi/vars.
  57. *
  58. * 12 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
  59. * Feedback received from Stephane Eranian incorporated.
  60. * efivar_write() checks copy_from_user() return value.
  61. * efivar_read/write() returns proper errno.
  62. * v0.02 release to linux-ia64@linuxia64.org
  63. *
  64. * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com>
  65. * v0.01 release to linux-ia64@linuxia64.org
  66. */
  67. #include <linux/capability.h>
  68. #include <linux/types.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/mm.h>
  72. #include <linux/module.h>
  73. #include <linux/string.h>
  74. #include <linux/smp.h>
  75. #include <linux/efi.h>
  76. #include <linux/sysfs.h>
  77. #include <linux/kobject.h>
  78. #include <linux/device.h>
  79. #include <linux/slab.h>
  80. #include <linux/ctype.h>
  81. #include <linux/fs.h>
  82. #include <linux/ramfs.h>
  83. #include <linux/pagemap.h>
  84. #include <asm/uaccess.h>
  85. #define EFIVARS_VERSION "0.08"
  86. #define EFIVARS_DATE "2004-May-17"
  87. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  88. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  89. MODULE_LICENSE("GPL");
  90. MODULE_VERSION(EFIVARS_VERSION);
  91. static LIST_HEAD(efivarfs_list);
  92. LIST_HEAD(efivar_sysfs_list);
  93. EXPORT_SYMBOL_GPL(efivar_sysfs_list);
  94. struct efivar_attribute {
  95. struct attribute attr;
  96. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  97. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  98. };
  99. /* Private pointer to registered efivars */
  100. static struct efivars *__efivars;
  101. static struct kset *efivars_kset;
  102. static struct bin_attribute *efivars_new_var;
  103. static struct bin_attribute *efivars_del_var;
  104. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  105. struct efivar_attribute efivar_attr_##_name = { \
  106. .attr = {.name = __stringify(_name), .mode = _mode}, \
  107. .show = _show, \
  108. .store = _store, \
  109. };
  110. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  111. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  112. /*
  113. * Prototype for sysfs creation function
  114. */
  115. static int
  116. efivar_create_sysfs_entry(struct efivar_entry *new_var);
  117. /*
  118. * Prototype for workqueue functions updating sysfs entry
  119. */
  120. static void efivar_update_sysfs_entries(struct work_struct *);
  121. static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
  122. static bool efivar_wq_enabled = true;
  123. static bool
  124. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  125. unsigned long len)
  126. {
  127. struct efi_generic_dev_path *node;
  128. int offset = 0;
  129. node = (struct efi_generic_dev_path *)buffer;
  130. if (len < sizeof(*node))
  131. return false;
  132. while (offset <= len - sizeof(*node) &&
  133. node->length >= sizeof(*node) &&
  134. node->length <= len - offset) {
  135. offset += node->length;
  136. if ((node->type == EFI_DEV_END_PATH ||
  137. node->type == EFI_DEV_END_PATH2) &&
  138. node->sub_type == EFI_DEV_END_ENTIRE)
  139. return true;
  140. node = (struct efi_generic_dev_path *)(buffer + offset);
  141. }
  142. /*
  143. * If we're here then either node->length pointed past the end
  144. * of the buffer or we reached the end of the buffer without
  145. * finding a device path end node.
  146. */
  147. return false;
  148. }
  149. static bool
  150. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  151. unsigned long len)
  152. {
  153. /* An array of 16-bit integers */
  154. if ((len % 2) != 0)
  155. return false;
  156. return true;
  157. }
  158. static bool
  159. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  160. unsigned long len)
  161. {
  162. u16 filepathlength;
  163. int i, desclength = 0, namelen;
  164. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  165. /* Either "Boot" or "Driver" followed by four digits of hex */
  166. for (i = match; i < match+4; i++) {
  167. if (var->VariableName[i] > 127 ||
  168. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  169. return true;
  170. }
  171. /* Reject it if there's 4 digits of hex and then further content */
  172. if (namelen > match + 4)
  173. return false;
  174. /* A valid entry must be at least 8 bytes */
  175. if (len < 8)
  176. return false;
  177. filepathlength = buffer[4] | buffer[5] << 8;
  178. /*
  179. * There's no stored length for the description, so it has to be
  180. * found by hand
  181. */
  182. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  183. /* Each boot entry must have a descriptor */
  184. if (!desclength)
  185. return false;
  186. /*
  187. * If the sum of the length of the description, the claimed filepath
  188. * length and the original header are greater than the length of the
  189. * variable, it's malformed
  190. */
  191. if ((desclength + filepathlength + 6) > len)
  192. return false;
  193. /*
  194. * And, finally, check the filepath
  195. */
  196. return validate_device_path(var, match, buffer + desclength + 6,
  197. filepathlength);
  198. }
  199. static bool
  200. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  201. unsigned long len)
  202. {
  203. /* A single 16-bit integer */
  204. if (len != 2)
  205. return false;
  206. return true;
  207. }
  208. static bool
  209. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  210. unsigned long len)
  211. {
  212. int i;
  213. for (i = 0; i < len; i++) {
  214. if (buffer[i] > 127)
  215. return false;
  216. if (buffer[i] == 0)
  217. return true;
  218. }
  219. return false;
  220. }
  221. struct variable_validate {
  222. char *name;
  223. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  224. unsigned long len);
  225. };
  226. static const struct variable_validate variable_validate[] = {
  227. { "BootNext", validate_uint16 },
  228. { "BootOrder", validate_boot_order },
  229. { "DriverOrder", validate_boot_order },
  230. { "Boot*", validate_load_option },
  231. { "Driver*", validate_load_option },
  232. { "ConIn", validate_device_path },
  233. { "ConInDev", validate_device_path },
  234. { "ConOut", validate_device_path },
  235. { "ConOutDev", validate_device_path },
  236. { "ErrOut", validate_device_path },
  237. { "ErrOutDev", validate_device_path },
  238. { "Timeout", validate_uint16 },
  239. { "Lang", validate_ascii_string },
  240. { "PlatformLang", validate_ascii_string },
  241. { "", NULL },
  242. };
  243. bool
  244. efivar_validate(struct efi_variable *var, u8 *data, unsigned long len)
  245. {
  246. int i;
  247. u16 *unicode_name = var->VariableName;
  248. for (i = 0; variable_validate[i].validate != NULL; i++) {
  249. const char *name = variable_validate[i].name;
  250. int match;
  251. for (match = 0; ; match++) {
  252. char c = name[match];
  253. u16 u = unicode_name[match];
  254. /* All special variables are plain ascii */
  255. if (u > 127)
  256. return true;
  257. /* Wildcard in the matching name means we've matched */
  258. if (c == '*')
  259. return variable_validate[i].validate(var,
  260. match, data, len);
  261. /* Case sensitive match */
  262. if (c != u)
  263. break;
  264. /* Reached the end of the string while matching */
  265. if (!c)
  266. return variable_validate[i].validate(var,
  267. match, data, len);
  268. }
  269. }
  270. return true;
  271. }
  272. EXPORT_SYMBOL_GPL(efivar_validate);
  273. static efi_status_t
  274. check_var_size(u32 attributes, unsigned long size)
  275. {
  276. u64 storage_size, remaining_size, max_size;
  277. efi_status_t status;
  278. const struct efivar_operations *fops = __efivars->ops;
  279. if (!fops->query_variable_info)
  280. return EFI_UNSUPPORTED;
  281. status = fops->query_variable_info(attributes, &storage_size,
  282. &remaining_size, &max_size);
  283. if (status != EFI_SUCCESS)
  284. return status;
  285. if (!storage_size || size > remaining_size || size > max_size ||
  286. (remaining_size - size) < (storage_size / 2))
  287. return EFI_OUT_OF_RESOURCES;
  288. return status;
  289. }
  290. static ssize_t
  291. efivar_guid_read(struct efivar_entry *entry, char *buf)
  292. {
  293. struct efi_variable *var = &entry->var;
  294. char *str = buf;
  295. if (!entry || !buf)
  296. return 0;
  297. efi_guid_unparse(&var->VendorGuid, str);
  298. str += strlen(str);
  299. str += sprintf(str, "\n");
  300. return str - buf;
  301. }
  302. static ssize_t
  303. efivar_attr_read(struct efivar_entry *entry, char *buf)
  304. {
  305. struct efi_variable *var = &entry->var;
  306. char *str = buf;
  307. if (!entry || !buf)
  308. return -EINVAL;
  309. var->DataSize = 1024;
  310. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  311. return -EIO;
  312. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  313. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  314. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  315. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  316. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  317. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  318. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  319. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  320. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  321. str += sprintf(str,
  322. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  323. if (var->Attributes &
  324. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  325. str += sprintf(str,
  326. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  327. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  328. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  329. return str - buf;
  330. }
  331. static ssize_t
  332. efivar_size_read(struct efivar_entry *entry, char *buf)
  333. {
  334. struct efi_variable *var = &entry->var;
  335. char *str = buf;
  336. if (!entry || !buf)
  337. return -EINVAL;
  338. var->DataSize = 1024;
  339. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  340. return -EIO;
  341. str += sprintf(str, "0x%lx\n", var->DataSize);
  342. return str - buf;
  343. }
  344. static ssize_t
  345. efivar_data_read(struct efivar_entry *entry, char *buf)
  346. {
  347. struct efi_variable *var = &entry->var;
  348. if (!entry || !buf)
  349. return -EINVAL;
  350. var->DataSize = 1024;
  351. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  352. return -EIO;
  353. memcpy(buf, var->Data, var->DataSize);
  354. return var->DataSize;
  355. }
  356. /*
  357. * We allow each variable to be edited via rewriting the
  358. * entire efi variable structure.
  359. */
  360. static ssize_t
  361. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  362. {
  363. struct efi_variable *new_var, *var = &entry->var;
  364. int err;
  365. if (count != sizeof(struct efi_variable))
  366. return -EINVAL;
  367. new_var = (struct efi_variable *)buf;
  368. /*
  369. * If only updating the variable data, then the name
  370. * and guid should remain the same
  371. */
  372. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  373. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  374. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  375. return -EINVAL;
  376. }
  377. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  378. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  379. return -EINVAL;
  380. }
  381. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  382. efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) {
  383. printk(KERN_ERR "efivars: Malformed variable content\n");
  384. return -EINVAL;
  385. }
  386. memcpy(&entry->var, new_var, count);
  387. err = efivar_entry_set(entry, new_var->Attributes,
  388. new_var->DataSize, new_var->Data, false);
  389. if (err) {
  390. printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
  391. return -EIO;
  392. }
  393. return count;
  394. }
  395. static ssize_t
  396. efivar_show_raw(struct efivar_entry *entry, char *buf)
  397. {
  398. struct efi_variable *var = &entry->var;
  399. if (!entry || !buf)
  400. return 0;
  401. var->DataSize = 1024;
  402. if (efivar_entry_get(entry, &entry->var.Attributes,
  403. &entry->var.DataSize, entry->var.Data))
  404. return -EIO;
  405. memcpy(buf, var, sizeof(*var));
  406. return sizeof(*var);
  407. }
  408. /*
  409. * Generic read/write functions that call the specific functions of
  410. * the attributes...
  411. */
  412. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  413. char *buf)
  414. {
  415. struct efivar_entry *var = to_efivar_entry(kobj);
  416. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  417. ssize_t ret = -EIO;
  418. if (!capable(CAP_SYS_ADMIN))
  419. return -EACCES;
  420. if (efivar_attr->show) {
  421. ret = efivar_attr->show(var, buf);
  422. }
  423. return ret;
  424. }
  425. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  426. const char *buf, size_t count)
  427. {
  428. struct efivar_entry *var = to_efivar_entry(kobj);
  429. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  430. ssize_t ret = -EIO;
  431. if (!capable(CAP_SYS_ADMIN))
  432. return -EACCES;
  433. if (efivar_attr->store)
  434. ret = efivar_attr->store(var, buf, count);
  435. return ret;
  436. }
  437. static const struct sysfs_ops efivar_attr_ops = {
  438. .show = efivar_attr_show,
  439. .store = efivar_attr_store,
  440. };
  441. static void efivar_release(struct kobject *kobj)
  442. {
  443. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  444. kfree(var);
  445. }
  446. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  447. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  448. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  449. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  450. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  451. static struct attribute *def_attrs[] = {
  452. &efivar_attr_guid.attr,
  453. &efivar_attr_size.attr,
  454. &efivar_attr_attributes.attr,
  455. &efivar_attr_data.attr,
  456. &efivar_attr_raw_var.attr,
  457. NULL,
  458. };
  459. static struct kobj_type efivar_ktype = {
  460. .release = efivar_release,
  461. .sysfs_ops = &efivar_attr_ops,
  462. .default_attrs = def_attrs,
  463. };
  464. static int efivarfs_file_open(struct inode *inode, struct file *file)
  465. {
  466. file->private_data = inode->i_private;
  467. return 0;
  468. }
  469. static int efi_status_to_err(efi_status_t status)
  470. {
  471. int err;
  472. switch (status) {
  473. case EFI_SUCCESS:
  474. err = 0;
  475. break;
  476. case EFI_INVALID_PARAMETER:
  477. err = -EINVAL;
  478. break;
  479. case EFI_OUT_OF_RESOURCES:
  480. err = -ENOSPC;
  481. break;
  482. case EFI_DEVICE_ERROR:
  483. err = -EIO;
  484. break;
  485. case EFI_WRITE_PROTECTED:
  486. err = -EROFS;
  487. break;
  488. case EFI_SECURITY_VIOLATION:
  489. err = -EACCES;
  490. break;
  491. case EFI_NOT_FOUND:
  492. err = -ENOENT;
  493. break;
  494. default:
  495. err = -EINVAL;
  496. }
  497. return err;
  498. }
  499. static ssize_t efivarfs_file_write(struct file *file,
  500. const char __user *userbuf, size_t count, loff_t *ppos)
  501. {
  502. struct efivar_entry *var = file->private_data;
  503. void *data;
  504. u32 attributes;
  505. struct inode *inode = file->f_mapping->host;
  506. unsigned long datasize = count - sizeof(attributes);
  507. ssize_t bytes = 0;
  508. bool set = false;
  509. if (count < sizeof(attributes))
  510. return -EINVAL;
  511. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  512. return -EFAULT;
  513. if (attributes & ~(EFI_VARIABLE_MASK))
  514. return -EINVAL;
  515. data = kmalloc(datasize, GFP_KERNEL);
  516. if (!data)
  517. return -ENOMEM;
  518. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  519. bytes = -EFAULT;
  520. goto out;
  521. }
  522. bytes = efivar_entry_set_get_size(var, attributes, &datasize,
  523. data, &set);
  524. if (!set && bytes)
  525. goto out;
  526. if (!bytes) {
  527. mutex_lock(&inode->i_mutex);
  528. i_size_write(inode, datasize + sizeof(attributes));
  529. mutex_unlock(&inode->i_mutex);
  530. } else if (bytes == -ENOENT) {
  531. drop_nlink(inode);
  532. d_delete(file->f_dentry);
  533. dput(file->f_dentry);
  534. } else
  535. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  536. "status=%zu\n", bytes);
  537. bytes = count;
  538. out:
  539. kfree(data);
  540. return bytes;
  541. }
  542. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  543. size_t count, loff_t *ppos)
  544. {
  545. struct efivar_entry *var = file->private_data;
  546. unsigned long datasize = 0;
  547. u32 attributes;
  548. void *data;
  549. ssize_t size = 0;
  550. int err;
  551. err = efivar_entry_size(var, &datasize);
  552. if (err)
  553. return err;
  554. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  555. if (!data)
  556. return -ENOMEM;
  557. size = efivar_entry_get(var, &attributes, &datasize,
  558. data + sizeof(attributes));
  559. if (size)
  560. goto out_free;
  561. memcpy(data, &attributes, sizeof(attributes));
  562. size = simple_read_from_buffer(userbuf, count, ppos,
  563. data, datasize + sizeof(attributes));
  564. out_free:
  565. kfree(data);
  566. return size;
  567. }
  568. static void efivarfs_evict_inode(struct inode *inode)
  569. {
  570. clear_inode(inode);
  571. }
  572. static const struct super_operations efivarfs_ops = {
  573. .statfs = simple_statfs,
  574. .drop_inode = generic_delete_inode,
  575. .evict_inode = efivarfs_evict_inode,
  576. .show_options = generic_show_options,
  577. };
  578. static struct super_block *efivarfs_sb;
  579. static const struct inode_operations efivarfs_dir_inode_operations;
  580. static const struct file_operations efivarfs_file_operations = {
  581. .open = efivarfs_file_open,
  582. .read = efivarfs_file_read,
  583. .write = efivarfs_file_write,
  584. .llseek = no_llseek,
  585. };
  586. static struct inode *efivarfs_get_inode(struct super_block *sb,
  587. const struct inode *dir, int mode, dev_t dev)
  588. {
  589. struct inode *inode = new_inode(sb);
  590. if (inode) {
  591. inode->i_ino = get_next_ino();
  592. inode->i_mode = mode;
  593. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  594. switch (mode & S_IFMT) {
  595. case S_IFREG:
  596. inode->i_fop = &efivarfs_file_operations;
  597. break;
  598. case S_IFDIR:
  599. inode->i_op = &efivarfs_dir_inode_operations;
  600. inode->i_fop = &simple_dir_operations;
  601. inc_nlink(inode);
  602. break;
  603. }
  604. }
  605. return inode;
  606. }
  607. /*
  608. * Return true if 'str' is a valid efivarfs filename of the form,
  609. *
  610. * VariableName-12345678-1234-1234-1234-1234567891bc
  611. */
  612. static bool efivarfs_valid_name(const char *str, int len)
  613. {
  614. static const char dashes[EFI_VARIABLE_GUID_LEN] = {
  615. [8] = 1, [13] = 1, [18] = 1, [23] = 1
  616. };
  617. const char *s = str + len - EFI_VARIABLE_GUID_LEN;
  618. int i;
  619. /*
  620. * We need a GUID, plus at least one letter for the variable name,
  621. * plus the '-' separator
  622. */
  623. if (len < EFI_VARIABLE_GUID_LEN + 2)
  624. return false;
  625. /* GUID must be preceded by a '-' */
  626. if (*(s - 1) != '-')
  627. return false;
  628. /*
  629. * Validate that 's' is of the correct format, e.g.
  630. *
  631. * 12345678-1234-1234-1234-123456789abc
  632. */
  633. for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) {
  634. if (dashes[i]) {
  635. if (*s++ != '-')
  636. return false;
  637. } else {
  638. if (!isxdigit(*s++))
  639. return false;
  640. }
  641. }
  642. return true;
  643. }
  644. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  645. {
  646. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  647. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  648. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  649. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  650. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  651. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  652. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  653. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  654. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  655. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  656. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  657. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  658. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  659. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  660. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  661. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  662. }
  663. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  664. umode_t mode, bool excl)
  665. {
  666. struct inode *inode;
  667. struct efivar_entry *var;
  668. int namelen, i = 0, err = 0;
  669. if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
  670. return -EINVAL;
  671. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  672. if (!inode)
  673. return -ENOMEM;
  674. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  675. if (!var) {
  676. err = -ENOMEM;
  677. goto out;
  678. }
  679. /* length of the variable name itself: remove GUID and separator */
  680. namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
  681. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  682. &var->var.VendorGuid);
  683. for (i = 0; i < namelen; i++)
  684. var->var.VariableName[i] = dentry->d_name.name[i];
  685. var->var.VariableName[i] = '\0';
  686. inode->i_private = var;
  687. efivar_entry_add(var, &efivarfs_list);
  688. d_instantiate(dentry, inode);
  689. dget(dentry);
  690. out:
  691. if (err) {
  692. kfree(var);
  693. iput(inode);
  694. }
  695. return err;
  696. }
  697. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  698. {
  699. struct efivar_entry *var = dentry->d_inode->i_private;
  700. if (efivar_entry_delete(var))
  701. return -EINVAL;
  702. drop_nlink(dentry->d_inode);
  703. dput(dentry);
  704. return 0;
  705. };
  706. /*
  707. * Compare two efivarfs file names.
  708. *
  709. * An efivarfs filename is composed of two parts,
  710. *
  711. * 1. A case-sensitive variable name
  712. * 2. A case-insensitive GUID
  713. *
  714. * So we need to perform a case-sensitive match on part 1 and a
  715. * case-insensitive match on part 2.
  716. */
  717. static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
  718. const struct dentry *dentry, const struct inode *inode,
  719. unsigned int len, const char *str,
  720. const struct qstr *name)
  721. {
  722. int guid = len - EFI_VARIABLE_GUID_LEN;
  723. if (name->len != len)
  724. return 1;
  725. /* Case-sensitive compare for the variable name */
  726. if (memcmp(str, name->name, guid))
  727. return 1;
  728. /* Case-insensitive compare for the GUID */
  729. return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
  730. }
  731. static int efivarfs_d_hash(const struct dentry *dentry,
  732. const struct inode *inode, struct qstr *qstr)
  733. {
  734. unsigned long hash = init_name_hash();
  735. const unsigned char *s = qstr->name;
  736. unsigned int len = qstr->len;
  737. if (!efivarfs_valid_name(s, len))
  738. return -EINVAL;
  739. while (len-- > EFI_VARIABLE_GUID_LEN)
  740. hash = partial_name_hash(*s++, hash);
  741. /* GUID is case-insensitive. */
  742. while (len--)
  743. hash = partial_name_hash(tolower(*s++), hash);
  744. qstr->hash = end_name_hash(hash);
  745. return 0;
  746. }
  747. /*
  748. * Retaining negative dentries for an in-memory filesystem just wastes
  749. * memory and lookup time: arrange for them to be deleted immediately.
  750. */
  751. static int efivarfs_delete_dentry(const struct dentry *dentry)
  752. {
  753. return 1;
  754. }
  755. static struct dentry_operations efivarfs_d_ops = {
  756. .d_compare = efivarfs_d_compare,
  757. .d_hash = efivarfs_d_hash,
  758. .d_delete = efivarfs_delete_dentry,
  759. };
  760. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  761. {
  762. struct dentry *d;
  763. struct qstr q;
  764. int err;
  765. q.name = name;
  766. q.len = strlen(name);
  767. err = efivarfs_d_hash(NULL, NULL, &q);
  768. if (err)
  769. return ERR_PTR(err);
  770. d = d_alloc(parent, &q);
  771. if (d)
  772. return d;
  773. return ERR_PTR(-ENOMEM);
  774. }
  775. static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
  776. unsigned long name_size, void *data)
  777. {
  778. struct super_block *sb = (struct super_block *)data;
  779. struct efivar_entry *entry;
  780. struct inode *inode = NULL;
  781. struct dentry *dentry, *root = sb->s_root;
  782. unsigned long size = 0;
  783. char *name;
  784. int len, i;
  785. int err = -ENOMEM;
  786. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  787. if (!entry)
  788. return err;
  789. memcpy(entry->var.VariableName, name16, name_size);
  790. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  791. len = utf16_strlen(entry->var.VariableName);
  792. /* name, plus '-', plus GUID, plus NUL*/
  793. name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
  794. if (!name)
  795. goto fail;
  796. for (i = 0; i < len; i++)
  797. name[i] = entry->var.VariableName[i] & 0xFF;
  798. name[len] = '-';
  799. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  800. name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
  801. inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
  802. if (!inode)
  803. goto fail_name;
  804. dentry = efivarfs_alloc_dentry(root, name);
  805. if (IS_ERR(dentry)) {
  806. err = PTR_ERR(dentry);
  807. goto fail_inode;
  808. }
  809. /* copied by the above to local storage in the dentry. */
  810. kfree(name);
  811. efivar_entry_size(entry, &size);
  812. efivar_entry_add(entry, &efivarfs_list);
  813. mutex_lock(&inode->i_mutex);
  814. inode->i_private = entry;
  815. i_size_write(inode, size + sizeof(entry->var.Attributes));
  816. mutex_unlock(&inode->i_mutex);
  817. d_add(dentry, inode);
  818. return 0;
  819. fail_inode:
  820. iput(inode);
  821. fail_name:
  822. kfree(name);
  823. fail:
  824. kfree(entry);
  825. return err;
  826. }
  827. static int efivarfs_destroy(struct efivar_entry *entry, void *data)
  828. {
  829. efivar_entry_remove(entry);
  830. kfree(entry);
  831. return 0;
  832. }
  833. static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  834. {
  835. struct inode *inode = NULL;
  836. struct dentry *root;
  837. int err;
  838. efivarfs_sb = sb;
  839. sb->s_maxbytes = MAX_LFS_FILESIZE;
  840. sb->s_blocksize = PAGE_CACHE_SIZE;
  841. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  842. sb->s_magic = EFIVARFS_MAGIC;
  843. sb->s_op = &efivarfs_ops;
  844. sb->s_d_op = &efivarfs_d_ops;
  845. sb->s_time_gran = 1;
  846. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  847. if (!inode)
  848. return -ENOMEM;
  849. inode->i_op = &efivarfs_dir_inode_operations;
  850. root = d_make_root(inode);
  851. sb->s_root = root;
  852. if (!root)
  853. return -ENOMEM;
  854. INIT_LIST_HEAD(&efivarfs_list);
  855. err = efivar_init(efivarfs_callback, (void *)sb, false,
  856. true, &efivarfs_list);
  857. if (err)
  858. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  859. return err;
  860. }
  861. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  862. int flags, const char *dev_name, void *data)
  863. {
  864. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  865. }
  866. static void efivarfs_kill_sb(struct super_block *sb)
  867. {
  868. kill_litter_super(sb);
  869. efivarfs_sb = NULL;
  870. /* Remove all entries and destroy */
  871. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  872. }
  873. static struct file_system_type efivarfs_type = {
  874. .name = "efivarfs",
  875. .mount = efivarfs_mount,
  876. .kill_sb = efivarfs_kill_sb,
  877. };
  878. MODULE_ALIAS_FS("efivarfs");
  879. /*
  880. * Handle negative dentry.
  881. */
  882. static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
  883. unsigned int flags)
  884. {
  885. if (dentry->d_name.len > NAME_MAX)
  886. return ERR_PTR(-ENAMETOOLONG);
  887. d_add(dentry, NULL);
  888. return NULL;
  889. }
  890. static const struct inode_operations efivarfs_dir_inode_operations = {
  891. .lookup = efivarfs_lookup,
  892. .unlink = efivarfs_unlink,
  893. .create = efivarfs_create,
  894. };
  895. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  896. struct bin_attribute *bin_attr,
  897. char *buf, loff_t pos, size_t count)
  898. {
  899. struct efi_variable *new_var = (struct efi_variable *)buf;
  900. struct efivar_entry *new_entry;
  901. int err;
  902. if (!capable(CAP_SYS_ADMIN))
  903. return -EACCES;
  904. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  905. efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) {
  906. printk(KERN_ERR "efivars: Malformed variable content\n");
  907. return -EINVAL;
  908. }
  909. new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
  910. if (!new_entry)
  911. return -ENOMEM;
  912. memcpy(&new_entry->var, new_var, sizeof(*new_var));
  913. err = efivar_entry_set(new_entry, new_var->Attributes, new_var->DataSize,
  914. new_var->Data, &efivar_sysfs_list);
  915. if (err) {
  916. if (err == -EEXIST)
  917. err = -EINVAL;
  918. goto out;
  919. }
  920. if (efivar_create_sysfs_entry(new_entry)) {
  921. printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
  922. kfree(new_entry);
  923. }
  924. return count;
  925. out:
  926. kfree(new_entry);
  927. return err;
  928. }
  929. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  930. struct bin_attribute *bin_attr,
  931. char *buf, loff_t pos, size_t count)
  932. {
  933. struct efi_variable *del_var = (struct efi_variable *)buf;
  934. struct efivar_entry *entry;
  935. int err = 0;
  936. if (!capable(CAP_SYS_ADMIN))
  937. return -EACCES;
  938. efivar_entry_iter_begin();
  939. entry = efivar_entry_find(del_var->VariableName, del_var->VendorGuid,
  940. &efivar_sysfs_list, true);
  941. if (!entry)
  942. err = -EINVAL;
  943. else if (__efivar_entry_delete(entry))
  944. err = -EIO;
  945. efivar_entry_iter_end();
  946. if (err)
  947. return err;
  948. efivar_unregister(entry);
  949. /* It's dead Jim.... */
  950. return count;
  951. }
  952. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
  953. struct list_head *head)
  954. {
  955. struct efivar_entry *entry, *n;
  956. unsigned long strsize1, strsize2;
  957. bool found = false;
  958. strsize1 = utf16_strsize(variable_name, 1024);
  959. list_for_each_entry_safe(entry, n, head, list) {
  960. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  961. if (strsize1 == strsize2 &&
  962. !memcmp(variable_name, &(entry->var.VariableName),
  963. strsize2) &&
  964. !efi_guidcmp(entry->var.VendorGuid,
  965. *vendor)) {
  966. found = true;
  967. break;
  968. }
  969. }
  970. return found;
  971. }
  972. static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
  973. unsigned long name_size, void *data)
  974. {
  975. struct efivar_entry *entry = data;
  976. if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
  977. return 0;
  978. memcpy(entry->var.VariableName, name, name_size);
  979. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  980. return 1;
  981. }
  982. /*
  983. * Returns the size of variable_name, in bytes, including the
  984. * terminating NULL character, or variable_name_size if no NULL
  985. * character is found among the first variable_name_size bytes.
  986. */
  987. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  988. unsigned long variable_name_size)
  989. {
  990. unsigned long len;
  991. efi_char16_t c;
  992. /*
  993. * The variable name is, by definition, a NULL-terminated
  994. * string, so make absolutely sure that variable_name_size is
  995. * the value we expect it to be. If not, return the real size.
  996. */
  997. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  998. c = variable_name[(len / sizeof(c)) - 1];
  999. if (!c)
  1000. break;
  1001. }
  1002. return min(len, variable_name_size);
  1003. }
  1004. static void efivar_update_sysfs_entries(struct work_struct *work)
  1005. {
  1006. struct efivar_entry *entry;
  1007. int err;
  1008. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1009. if (!entry)
  1010. return;
  1011. /* Add new sysfs entries */
  1012. while (1) {
  1013. memset(entry, 0, sizeof(*entry));
  1014. err = efivar_init(efivar_update_sysfs_entry, entry,
  1015. true, false, &efivar_sysfs_list);
  1016. if (!err)
  1017. break;
  1018. efivar_create_sysfs_entry(entry);
  1019. }
  1020. kfree(entry);
  1021. }
  1022. /*
  1023. * Let's not leave out systab information that snuck into
  1024. * the efivars driver
  1025. */
  1026. static ssize_t systab_show(struct kobject *kobj,
  1027. struct kobj_attribute *attr, char *buf)
  1028. {
  1029. char *str = buf;
  1030. if (!kobj || !buf)
  1031. return -EINVAL;
  1032. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1033. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1034. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1035. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1036. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1037. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1038. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1039. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1040. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1041. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1042. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1043. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1044. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1045. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1046. return str - buf;
  1047. }
  1048. static struct kobj_attribute efi_attr_systab =
  1049. __ATTR(systab, 0400, systab_show, NULL);
  1050. static struct attribute *efi_subsys_attrs[] = {
  1051. &efi_attr_systab.attr,
  1052. NULL, /* maybe more in the future? */
  1053. };
  1054. static struct attribute_group efi_subsys_attr_group = {
  1055. .attrs = efi_subsys_attrs,
  1056. };
  1057. static struct kobject *efi_kobj;
  1058. /**
  1059. * efivar_create_sysfs_entry - create a new entry in sysfs
  1060. * @new_var: efivar entry to create
  1061. *
  1062. * Returns 1 on failure, 0 on success
  1063. */
  1064. static int
  1065. efivar_create_sysfs_entry(struct efivar_entry *new_var)
  1066. {
  1067. int i, short_name_size;
  1068. char *short_name;
  1069. unsigned long variable_name_size;
  1070. efi_char16_t *variable_name;
  1071. variable_name = new_var->var.VariableName;
  1072. variable_name_size = utf16_strlen(variable_name) * sizeof(efi_char16_t);
  1073. /*
  1074. * Length of the variable bytes in ASCII, plus the '-' separator,
  1075. * plus the GUID, plus trailing NUL
  1076. */
  1077. short_name_size = variable_name_size / sizeof(efi_char16_t)
  1078. + 1 + EFI_VARIABLE_GUID_LEN + 1;
  1079. short_name = kzalloc(short_name_size, GFP_KERNEL);
  1080. if (!short_name) {
  1081. kfree(short_name);
  1082. return 1;
  1083. }
  1084. /* Convert Unicode to normal chars (assume top bits are 0),
  1085. ala UTF-8 */
  1086. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1087. short_name[i] = variable_name[i] & 0xFF;
  1088. }
  1089. /* This is ugly, but necessary to separate one vendor's
  1090. private variables from another's. */
  1091. *(short_name + strlen(short_name)) = '-';
  1092. efi_guid_unparse(&new_var->var.VendorGuid,
  1093. short_name + strlen(short_name));
  1094. new_var->kobj.kset = efivars_kset;
  1095. i = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
  1096. NULL, "%s", short_name);
  1097. kfree(short_name);
  1098. if (i)
  1099. return 1;
  1100. kobject_uevent(&new_var->kobj, KOBJ_ADD);
  1101. efivar_entry_add(new_var, &efivar_sysfs_list);
  1102. return 0;
  1103. }
  1104. static int
  1105. create_efivars_bin_attributes(void)
  1106. {
  1107. struct bin_attribute *attr;
  1108. int error;
  1109. /* new_var */
  1110. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1111. if (!attr)
  1112. return -ENOMEM;
  1113. attr->attr.name = "new_var";
  1114. attr->attr.mode = 0200;
  1115. attr->write = efivar_create;
  1116. efivars_new_var = attr;
  1117. /* del_var */
  1118. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1119. if (!attr) {
  1120. error = -ENOMEM;
  1121. goto out_free;
  1122. }
  1123. attr->attr.name = "del_var";
  1124. attr->attr.mode = 0200;
  1125. attr->write = efivar_delete;
  1126. efivars_del_var = attr;
  1127. sysfs_bin_attr_init(efivars_new_var);
  1128. sysfs_bin_attr_init(efivars_del_var);
  1129. /* Register */
  1130. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
  1131. if (error) {
  1132. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1133. " due to error %d\n", error);
  1134. goto out_free;
  1135. }
  1136. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
  1137. if (error) {
  1138. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1139. " due to error %d\n", error);
  1140. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  1141. goto out_free;
  1142. }
  1143. return 0;
  1144. out_free:
  1145. kfree(efivars_del_var);
  1146. efivars_del_var = NULL;
  1147. kfree(efivars_new_var);
  1148. efivars_new_var = NULL;
  1149. return error;
  1150. }
  1151. static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
  1152. unsigned long name_size, void *data)
  1153. {
  1154. struct efivar_entry *entry;
  1155. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1156. if (!entry)
  1157. return -ENOMEM;
  1158. memcpy(entry->var.VariableName, name, name_size);
  1159. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  1160. efivar_create_sysfs_entry(entry);
  1161. return 0;
  1162. }
  1163. static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
  1164. {
  1165. efivar_entry_remove(entry);
  1166. efivar_unregister(entry);
  1167. return 0;
  1168. }
  1169. /*
  1170. * Print a warning when duplicate EFI variables are encountered and
  1171. * disable the sysfs workqueue since the firmware is buggy.
  1172. */
  1173. static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
  1174. unsigned long len16)
  1175. {
  1176. size_t i, len8 = len16 / sizeof(efi_char16_t);
  1177. char *s8;
  1178. /*
  1179. * Disable the workqueue since the algorithm it uses for
  1180. * detecting new variables won't work with this buggy
  1181. * implementation of GetNextVariableName().
  1182. */
  1183. efivar_wq_enabled = false;
  1184. s8 = kzalloc(len8, GFP_KERNEL);
  1185. if (!s8)
  1186. return;
  1187. for (i = 0; i < len8; i++)
  1188. s8[i] = s16[i];
  1189. printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
  1190. s8, vendor_guid);
  1191. kfree(s8);
  1192. }
  1193. static struct kobject *efivars_kobj;
  1194. void efivars_sysfs_exit(void)
  1195. {
  1196. /* Remove all entries and destroy */
  1197. __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
  1198. if (efivars_new_var)
  1199. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  1200. if (efivars_del_var)
  1201. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
  1202. kfree(efivars_new_var);
  1203. kfree(efivars_del_var);
  1204. kobject_put(efivars_kobj);
  1205. kset_unregister(efivars_kset);
  1206. }
  1207. int efivars_sysfs_init(void)
  1208. {
  1209. struct kobject *parent_kobj = efivars_kobject();
  1210. int error = 0;
  1211. /* No efivars has been registered yet */
  1212. if (!parent_kobj)
  1213. return 0;
  1214. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1215. EFIVARS_DATE);
  1216. efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
  1217. if (!efivars_kset) {
  1218. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1219. return -ENOMEM;
  1220. }
  1221. efivars_kobj = kobject_create_and_add("efivars", parent_kobj);
  1222. if (!efivars_kobj) {
  1223. pr_err("efivars: Subsystem registration failed.\n");
  1224. kset_unregister(efivars_kset);
  1225. return -ENOMEM;
  1226. }
  1227. efivar_init(efivars_sysfs_callback, NULL, false,
  1228. true, &efivar_sysfs_list);
  1229. error = create_efivars_bin_attributes();
  1230. if (error)
  1231. efivars_sysfs_exit();
  1232. return error;
  1233. }
  1234. EXPORT_SYMBOL_GPL(efivars_sysfs_init);
  1235. /**
  1236. * efivar_init - build the initial list of EFI variables
  1237. * @func: callback function to invoke for every variable
  1238. * @data: function-specific data to pass to @func
  1239. * @atomic: do we need to execute the @func-loop atomically?
  1240. * @duplicates: error if we encounter duplicates on @head?
  1241. * @head: initialised head of variable list
  1242. *
  1243. * Get every EFI variable from the firmware and invoke @func. @func
  1244. * should call efivar_entry_add() to build the list of variables.
  1245. *
  1246. * Returns 0 on success, or a kernel error code on failure.
  1247. */
  1248. int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
  1249. void *data, bool atomic, bool duplicates,
  1250. struct list_head *head)
  1251. {
  1252. const struct efivar_operations *ops = __efivars->ops;
  1253. unsigned long variable_name_size = 1024;
  1254. efi_char16_t *variable_name;
  1255. efi_status_t status;
  1256. efi_guid_t vendor_guid;
  1257. int err = 0;
  1258. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1259. if (!variable_name) {
  1260. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1261. return -ENOMEM;
  1262. }
  1263. spin_lock_irq(&__efivars->lock);
  1264. /*
  1265. * Per EFI spec, the maximum storage allocated for both
  1266. * the variable name and variable data is 1024 bytes.
  1267. */
  1268. do {
  1269. variable_name_size = 1024;
  1270. status = ops->get_next_variable(&variable_name_size,
  1271. variable_name,
  1272. &vendor_guid);
  1273. switch (status) {
  1274. case EFI_SUCCESS:
  1275. if (!atomic)
  1276. spin_unlock_irq(&__efivars->lock);
  1277. variable_name_size = var_name_strnsize(variable_name,
  1278. variable_name_size);
  1279. /*
  1280. * Some firmware implementations return the
  1281. * same variable name on multiple calls to
  1282. * get_next_variable(). Terminate the loop
  1283. * immediately as there is no guarantee that
  1284. * we'll ever see a different variable name,
  1285. * and may end up looping here forever.
  1286. */
  1287. if (duplicates &&
  1288. variable_is_present(variable_name, &vendor_guid, head)) {
  1289. dup_variable_bug(variable_name, &vendor_guid,
  1290. variable_name_size);
  1291. if (!atomic)
  1292. spin_lock_irq(&__efivars->lock);
  1293. status = EFI_NOT_FOUND;
  1294. break;
  1295. }
  1296. err = func(variable_name, vendor_guid, variable_name_size, data);
  1297. if (err)
  1298. status = EFI_NOT_FOUND;
  1299. if (!atomic)
  1300. spin_lock_irq(&__efivars->lock);
  1301. break;
  1302. case EFI_NOT_FOUND:
  1303. break;
  1304. default:
  1305. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1306. status);
  1307. status = EFI_NOT_FOUND;
  1308. break;
  1309. }
  1310. } while (status != EFI_NOT_FOUND);
  1311. spin_unlock_irq(&__efivars->lock);
  1312. kfree(variable_name);
  1313. return err;
  1314. }
  1315. EXPORT_SYMBOL_GPL(efivar_init);
  1316. /**
  1317. * efivar_entry_add - add entry to variable list
  1318. * @entry: entry to add to list
  1319. * @head: list head
  1320. */
  1321. void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
  1322. {
  1323. spin_lock_irq(&__efivars->lock);
  1324. list_add(&entry->list, head);
  1325. spin_unlock_irq(&__efivars->lock);
  1326. }
  1327. EXPORT_SYMBOL_GPL(efivar_entry_add);
  1328. /**
  1329. * efivar_entry_remove - remove entry from variable list
  1330. * @entry: entry to remove from list
  1331. */
  1332. void efivar_entry_remove(struct efivar_entry *entry)
  1333. {
  1334. spin_lock_irq(&__efivars->lock);
  1335. list_del(&entry->list);
  1336. spin_unlock_irq(&__efivars->lock);
  1337. }
  1338. EXPORT_SYMBOL_GPL(efivar_entry_remove);
  1339. /*
  1340. * efivar_entry_list_del_unlock - remove entry from variable list
  1341. * @entry: entry to remove
  1342. *
  1343. * Remove @entry from the variable list and release the list lock.
  1344. *
  1345. * NOTE: slightly weird locking semantics here - we expect to be
  1346. * called with the efivars lock already held, and we release it before
  1347. * returning. This is because this function is usually called after
  1348. * set_variable() while the lock is still held.
  1349. */
  1350. static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
  1351. {
  1352. WARN_ON(!spin_is_locked(&__efivars->lock));
  1353. list_del(&entry->list);
  1354. spin_unlock_irq(&__efivars->lock);
  1355. }
  1356. /**
  1357. * __efivar_entry_delete - delete an EFI variable
  1358. * @entry: entry containing EFI variable to delete
  1359. *
  1360. * Delete the variable from the firmware and remove @entry from the
  1361. * variable list. It is the caller's responsibility to free @entry
  1362. * once we return.
  1363. *
  1364. * This function differs from efivar_entry_delete() because it is
  1365. * safe to be called from within a efivar_entry_iter_begin() and
  1366. * efivar_entry_iter_end() region, unlike efivar_entry_delete().
  1367. *
  1368. * Returns 0 on success, or a converted EFI status code if
  1369. * set_variable() fails. If set_variable() fails the entry remains
  1370. * on the list.
  1371. */
  1372. int __efivar_entry_delete(struct efivar_entry *entry)
  1373. {
  1374. const struct efivar_operations *ops = __efivars->ops;
  1375. efi_status_t status;
  1376. WARN_ON(!spin_is_locked(&__efivars->lock));
  1377. status = ops->set_variable(entry->var.VariableName,
  1378. &entry->var.VendorGuid,
  1379. 0, 0, NULL);
  1380. if (status)
  1381. return efi_status_to_err(status);
  1382. list_del(&entry->list);
  1383. return 0;
  1384. }
  1385. EXPORT_SYMBOL_GPL(__efivar_entry_delete);
  1386. /**
  1387. * efivar_entry_delete - delete variable and remove entry from list
  1388. * @entry: entry containing variable to delete
  1389. *
  1390. * Delete the variable from the firmware and remove @entry from the
  1391. * variable list. It is the caller's responsibility to free @entry
  1392. * once we return.
  1393. *
  1394. * Returns 0 on success, or a converted EFI status code if
  1395. * set_variable() fails.
  1396. */
  1397. int efivar_entry_delete(struct efivar_entry *entry)
  1398. {
  1399. const struct efivar_operations *ops = __efivars->ops;
  1400. efi_status_t status;
  1401. spin_lock_irq(&__efivars->lock);
  1402. status = ops->set_variable(entry->var.VariableName,
  1403. &entry->var.VendorGuid,
  1404. 0, 0, NULL);
  1405. if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
  1406. spin_unlock_irq(&__efivars->lock);
  1407. return efi_status_to_err(status);
  1408. }
  1409. efivar_entry_list_del_unlock(entry);
  1410. return 0;
  1411. }
  1412. EXPORT_SYMBOL_GPL(efivar_entry_delete);
  1413. /**
  1414. * efivar_entry_set - call set_variable()
  1415. * @entry: entry containing the EFI variable to write
  1416. * @attributes: variable attributes
  1417. * @size: size of @data buffer
  1418. * @data: buffer containing variable data
  1419. * @head: head of variable list
  1420. *
  1421. * Calls set_variable() for an EFI variable. If creating a new EFI
  1422. * variable, this function is usually followed by efivar_entry_add().
  1423. *
  1424. * Before writing the variable, the remaining EFI variable storage
  1425. * space is checked to ensure there is enough room available.
  1426. *
  1427. * If @head is not NULL a lookup is performed to determine whether
  1428. * the entry is already on the list.
  1429. *
  1430. * Returns 0 on success, -EEXIST if a lookup is performed and the entry
  1431. * already exists on the list, or a converted EFI status code if
  1432. * set_variable() fails.
  1433. */
  1434. int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
  1435. unsigned long size, void *data, struct list_head *head)
  1436. {
  1437. const struct efivar_operations *ops = __efivars->ops;
  1438. efi_status_t status;
  1439. efi_char16_t *name = entry->var.VariableName;
  1440. efi_guid_t vendor = entry->var.VendorGuid;
  1441. spin_lock_irq(&__efivars->lock);
  1442. if (head && efivar_entry_find(name, vendor, head, false)) {
  1443. spin_unlock_irq(&__efivars->lock);
  1444. return -EEXIST;
  1445. }
  1446. status = check_var_size(attributes, size + utf16_strsize(name, 1024));
  1447. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  1448. status = ops->set_variable(name, &vendor,
  1449. attributes, size, data);
  1450. spin_unlock_irq(&__efivars->lock);
  1451. return efi_status_to_err(status);
  1452. }
  1453. EXPORT_SYMBOL_GPL(efivar_entry_set);
  1454. /**
  1455. * efivar_entry_set_safe - call set_variable() if enough space in firmware
  1456. * @name: buffer containing the variable name
  1457. * @vendor: variable vendor guid
  1458. * @attributes: variable attributes
  1459. * @block: can we block in this context?
  1460. * @size: size of @data buffer
  1461. * @data: buffer containing variable data
  1462. *
  1463. * Ensures there is enough free storage in the firmware for this variable, and
  1464. * if so, calls set_variable(). If creating a new EFI variable, this function
  1465. * is usually followed by efivar_entry_add().
  1466. *
  1467. * Returns 0 on success, -ENOSPC if the firmware does not have enough
  1468. * space for set_variable() to succeed, or a converted EFI status code
  1469. * if set_variable() fails.
  1470. */
  1471. int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
  1472. bool block, unsigned long size, void *data)
  1473. {
  1474. const struct efivar_operations *ops = __efivars->ops;
  1475. unsigned long flags;
  1476. efi_status_t status;
  1477. if (!ops->query_variable_info)
  1478. return -ENOSYS;
  1479. if (!block && !spin_trylock_irqsave(&__efivars->lock, flags))
  1480. return -EBUSY;
  1481. else
  1482. spin_lock_irqsave(&__efivars->lock, flags);
  1483. status = check_var_size(attributes, size + utf16_strsize(name, 1024));
  1484. if (status != EFI_SUCCESS) {
  1485. spin_unlock_irqrestore(&__efivars->lock, flags);
  1486. return -ENOSPC;
  1487. }
  1488. status = ops->set_variable(name, &vendor, attributes, size, data);
  1489. spin_unlock_irqrestore(&__efivars->lock, flags);
  1490. return efi_status_to_err(status);
  1491. }
  1492. EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
  1493. /**
  1494. * efivar_entry_find - search for an entry
  1495. * @name: the EFI variable name
  1496. * @guid: the EFI variable vendor's guid
  1497. * @head: head of the variable list
  1498. * @remove: should we remove the entry from the list?
  1499. *
  1500. * Search for an entry on the variable list that has the EFI variable
  1501. * name @name and vendor guid @guid. If an entry is found on the list
  1502. * and @remove is true, the entry is removed from the list.
  1503. *
  1504. * The caller MUST call efivar_entry_iter_begin() and
  1505. * efivar_entry_iter_end() before and after the invocation of this
  1506. * function, respectively.
  1507. *
  1508. * Returns the entry if found on the list, %NULL otherwise.
  1509. */
  1510. struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
  1511. struct list_head *head, bool remove)
  1512. {
  1513. struct efivar_entry *entry, *n;
  1514. int strsize1, strsize2;
  1515. bool found = false;
  1516. WARN_ON(!spin_is_locked(&__efivars->lock));
  1517. list_for_each_entry_safe(entry, n, head, list) {
  1518. strsize1 = utf16_strsize(name, 1024);
  1519. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  1520. if (strsize1 == strsize2 &&
  1521. !memcmp(name, &(entry->var.VariableName), strsize1) &&
  1522. !efi_guidcmp(guid, entry->var.VendorGuid)) {
  1523. found = true;
  1524. break;
  1525. }
  1526. }
  1527. if (!found)
  1528. return NULL;
  1529. if (remove)
  1530. list_del(&entry->list);
  1531. return entry;
  1532. }
  1533. EXPORT_SYMBOL_GPL(efivar_entry_find);
  1534. /**
  1535. * __efivar_entry_size - obtain the size of a variable
  1536. * @entry: entry for this variable
  1537. * @size: location to store the variable's size
  1538. *
  1539. * The caller MUST call efivar_entry_iter_begin() and
  1540. * efivar_entry_iter_end() before and after the invocation of this
  1541. * function, respectively.
  1542. */
  1543. int __efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  1544. {
  1545. const struct efivar_operations *ops = __efivars->ops;
  1546. efi_status_t status;
  1547. WARN_ON(!spin_is_locked(&__efivars->lock));
  1548. *size = 0;
  1549. status = ops->get_variable(entry->var.VariableName,
  1550. &entry->var.VendorGuid, NULL, size, NULL);
  1551. if (status != EFI_BUFFER_TOO_SMALL)
  1552. return efi_status_to_err(status);
  1553. return 0;
  1554. }
  1555. EXPORT_SYMBOL_GPL(__efivar_entry_size);
  1556. /**
  1557. * efivar_entry_size - obtain the size of a variable
  1558. * @entry: entry for this variable
  1559. * @size: location to store the variable's size
  1560. */
  1561. int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  1562. {
  1563. const struct efivar_operations *ops = __efivars->ops;
  1564. efi_status_t status;
  1565. *size = 0;
  1566. spin_lock_irq(&__efivars->lock);
  1567. status = ops->get_variable(entry->var.VariableName,
  1568. &entry->var.VendorGuid, NULL, size, NULL);
  1569. spin_unlock_irq(&__efivars->lock);
  1570. if (status != EFI_BUFFER_TOO_SMALL)
  1571. return efi_status_to_err(status);
  1572. return 0;
  1573. }
  1574. EXPORT_SYMBOL_GPL(efivar_entry_size);
  1575. /**
  1576. * efivar_entry_get - call get_variable()
  1577. * @entry: read data for this variable
  1578. * @attributes: variable attributes
  1579. * @size: size of @data buffer
  1580. * @data: buffer to store variable data
  1581. */
  1582. int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  1583. unsigned long *size, void *data)
  1584. {
  1585. const struct efivar_operations *ops = __efivars->ops;
  1586. efi_status_t status;
  1587. spin_lock_irq(&__efivars->lock);
  1588. status = ops->get_variable(entry->var.VariableName,
  1589. &entry->var.VendorGuid,
  1590. attributes, size, data);
  1591. spin_unlock_irq(&__efivars->lock);
  1592. return efi_status_to_err(status);
  1593. }
  1594. EXPORT_SYMBOL_GPL(efivar_entry_get);
  1595. /**
  1596. * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
  1597. * @entry: entry containing variable to set and get
  1598. * @attributes: attributes of variable to be written
  1599. * @size: size of data buffer
  1600. * @data: buffer containing data to write
  1601. * @set: did the set_variable() call succeed?
  1602. *
  1603. * This is a pretty special (complex) function. See efivarfs_file_write().
  1604. *
  1605. * Atomically call set_variable() for @entry and if the call is
  1606. * successful, return the new size of the variable from get_variable()
  1607. * in @size. The success of set_variable() is indicated by @set.
  1608. *
  1609. * Returns 0 on success, -EINVAL if the variable data is invalid,
  1610. * -ENOSPC if the firmware does not have enough available space, or a
  1611. * converted EFI status code if either of set_variable() or
  1612. * get_variable() fail.
  1613. *
  1614. * If the EFI variable does not exist when calling set_variable()
  1615. * (EFI_NOT_FOUND), @entry is removed from the variable list.
  1616. */
  1617. int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
  1618. unsigned long *size, void *data, bool *set)
  1619. {
  1620. const struct efivar_operations *ops = __efivars->ops;
  1621. efi_char16_t *name = entry->var.VariableName;
  1622. efi_guid_t *vendor = &entry->var.VendorGuid;
  1623. efi_status_t status;
  1624. int err;
  1625. *set = false;
  1626. if (efivar_validate(&entry->var, data, *size) == false)
  1627. return -EINVAL;
  1628. /*
  1629. * The lock here protects the get_variable call, the conditional
  1630. * set_variable call, and removal of the variable from the efivars
  1631. * list (in the case of an authenticated delete).
  1632. */
  1633. spin_lock_irq(&__efivars->lock);
  1634. /*
  1635. * Ensure that the available space hasn't shrunk below the safe level
  1636. */
  1637. status = check_var_size(attributes, *size + utf16_strsize(name, 1024));
  1638. if (status != EFI_SUCCESS) {
  1639. if (status != EFI_UNSUPPORTED) {
  1640. err = efi_status_to_err(status);
  1641. goto out;
  1642. }
  1643. if (*size > 65536) {
  1644. err = -ENOSPC;
  1645. goto out;
  1646. }
  1647. }
  1648. status = ops->set_variable(name, vendor, attributes, *size, data);
  1649. if (status != EFI_SUCCESS) {
  1650. err = efi_status_to_err(status);
  1651. goto out;
  1652. }
  1653. *set = true;
  1654. /*
  1655. * Writing to the variable may have caused a change in size (which
  1656. * could either be an append or an overwrite), or the variable to be
  1657. * deleted. Perform a GetVariable() so we can tell what actually
  1658. * happened.
  1659. */
  1660. *size = 0;
  1661. status = ops->get_variable(entry->var.VariableName,
  1662. &entry->var.VendorGuid,
  1663. NULL, size, NULL);
  1664. if (status == EFI_NOT_FOUND)
  1665. efivar_entry_list_del_unlock(entry);
  1666. else
  1667. spin_unlock_irq(&__efivars->lock);
  1668. if (status && status != EFI_BUFFER_TOO_SMALL)
  1669. return efi_status_to_err(status);
  1670. return 0;
  1671. out:
  1672. spin_unlock_irq(&__efivars->lock);
  1673. return err;
  1674. }
  1675. EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  1676. /**
  1677. * efivar_entry_iter_begin - begin iterating the variable list
  1678. *
  1679. * Lock the variable list to prevent entry insertion and removal until
  1680. * efivar_entry_iter_end() is called. This function is usually used in
  1681. * conjunction with __efivar_entry_iter() or efivar_entry_iter().
  1682. */
  1683. void efivar_entry_iter_begin(void)
  1684. {
  1685. spin_lock_irq(&__efivars->lock);
  1686. }
  1687. EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  1688. /**
  1689. * efivar_entry_iter_end - finish iterating the variable list
  1690. *
  1691. * Unlock the variable list and allow modifications to the list again.
  1692. */
  1693. void efivar_entry_iter_end(void)
  1694. {
  1695. spin_unlock_irq(&__efivars->lock);
  1696. }
  1697. EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
  1698. /**
  1699. * __efivar_entry_iter - iterate over variable list
  1700. * @func: callback function
  1701. * @head: head of the variable list
  1702. * @data: function-specific data to pass to callback
  1703. * @prev: entry to begin iterating from
  1704. *
  1705. * Iterate over the list of EFI variables and call @func with every
  1706. * entry on the list. It is safe for @func to remove entries in the
  1707. * list via efivar_entry_delete().
  1708. *
  1709. * You MUST call efivar_enter_iter_begin() before this function, and
  1710. * efivar_entry_iter_end() afterwards.
  1711. *
  1712. * It is possible to begin iteration from an arbitrary entry within
  1713. * the list by passing @prev. @prev is updated on return to point to
  1714. * the last entry passed to @func. To begin iterating from the
  1715. * beginning of the list @prev must be %NULL.
  1716. *
  1717. * The restrictions for @func are the same as documented for
  1718. * efivar_entry_iter().
  1719. */
  1720. int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  1721. struct list_head *head, void *data,
  1722. struct efivar_entry **prev)
  1723. {
  1724. struct efivar_entry *entry, *n;
  1725. int err = 0;
  1726. if (!prev || !*prev) {
  1727. list_for_each_entry_safe(entry, n, head, list) {
  1728. err = func(entry, data);
  1729. if (err)
  1730. break;
  1731. }
  1732. if (prev)
  1733. *prev = entry;
  1734. return err;
  1735. }
  1736. list_for_each_entry_safe_continue((*prev), n, head, list) {
  1737. err = func(*prev, data);
  1738. if (err)
  1739. break;
  1740. }
  1741. return err;
  1742. }
  1743. EXPORT_SYMBOL_GPL(__efivar_entry_iter);
  1744. /**
  1745. * efivar_entry_iter - iterate over variable list
  1746. * @func: callback function
  1747. * @head: head of variable list
  1748. * @data: function-specific data to pass to callback
  1749. *
  1750. * Iterate over the list of EFI variables and call @func with every
  1751. * entry on the list. It is safe for @func to remove entries in the
  1752. * list via efivar_entry_delete() while iterating.
  1753. *
  1754. * Some notes for the callback function:
  1755. * - a non-zero return value indicates an error and terminates the loop
  1756. * - @func is called from atomic context
  1757. */
  1758. int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  1759. struct list_head *head, void *data)
  1760. {
  1761. int err = 0;
  1762. efivar_entry_iter_begin();
  1763. err = __efivar_entry_iter(func, head, data, NULL);
  1764. efivar_entry_iter_end();
  1765. return err;
  1766. }
  1767. EXPORT_SYMBOL_GPL(efivar_entry_iter);
  1768. /**
  1769. * efivars_kobject - get the kobject for the registered efivars
  1770. *
  1771. * If efivars_register() has not been called we return NULL,
  1772. * otherwise return the kobject used at registration time.
  1773. */
  1774. struct kobject *efivars_kobject(void)
  1775. {
  1776. if (!__efivars)
  1777. return NULL;
  1778. return __efivars->kobject;
  1779. }
  1780. EXPORT_SYMBOL_GPL(efivars_kobject);
  1781. /**
  1782. * efivar_run_worker - schedule the efivar worker thread
  1783. */
  1784. void efivar_run_worker(void)
  1785. {
  1786. if (efivar_wq_enabled)
  1787. schedule_work(&efivar_work);
  1788. }
  1789. EXPORT_SYMBOL_GPL(efivar_run_worker);
  1790. /**
  1791. * efivars_register - register an efivars
  1792. * @efivars: efivars to register
  1793. * @ops: efivars operations
  1794. * @kobject: @efivars-specific kobject
  1795. *
  1796. * Only a single efivars can be registered at any time.
  1797. */
  1798. int efivars_register(struct efivars *efivars,
  1799. const struct efivar_operations *ops,
  1800. struct kobject *kobject)
  1801. {
  1802. spin_lock_init(&efivars->lock);
  1803. efivars->ops = ops;
  1804. efivars->kobject = kobject;
  1805. __efivars = efivars;
  1806. register_filesystem(&efivarfs_type);
  1807. return 0;
  1808. }
  1809. EXPORT_SYMBOL_GPL(efivars_register);
  1810. /**
  1811. * efivars_unregister - unregister an efivars
  1812. * @efivars: efivars to unregister
  1813. *
  1814. * The caller must have already removed every entry from the list,
  1815. * failure to do so is an error.
  1816. */
  1817. int efivars_unregister(struct efivars *efivars)
  1818. {
  1819. int rv;
  1820. if (!__efivars) {
  1821. printk(KERN_ERR "efivars not registered\n");
  1822. rv = -EINVAL;
  1823. goto out;
  1824. }
  1825. if (__efivars != efivars) {
  1826. rv = -EINVAL;
  1827. goto out;
  1828. }
  1829. __efivars = NULL;
  1830. rv = 0;
  1831. out:
  1832. return rv;
  1833. }
  1834. EXPORT_SYMBOL_GPL(efivars_unregister);
  1835. static struct efivars generic_efivars;
  1836. static struct efivar_operations generic_ops;
  1837. static int generic_ops_register(void)
  1838. {
  1839. int error;
  1840. generic_ops.get_variable = efi.get_variable;
  1841. generic_ops.set_variable = efi.set_variable;
  1842. generic_ops.get_next_variable = efi.get_next_variable;
  1843. generic_ops.query_variable_info = efi.query_variable_info;
  1844. error = efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  1845. if (error)
  1846. return error;
  1847. error = efivars_sysfs_init();
  1848. if (error)
  1849. efivars_unregister(&generic_efivars);
  1850. return error;
  1851. }
  1852. static void generic_ops_unregister(void)
  1853. {
  1854. efivars_sysfs_exit();
  1855. efivars_unregister(&generic_efivars);
  1856. }
  1857. /*
  1858. * For now we register the efi subsystem with the firmware subsystem
  1859. * and the vars subsystem with the efi subsystem. In the future, it
  1860. * might make sense to split off the efi subsystem into its own
  1861. * driver, but for now only efivars will register with it, so just
  1862. * include it here.
  1863. */
  1864. static int __init
  1865. efivars_init(void)
  1866. {
  1867. int error;
  1868. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  1869. return 0;
  1870. /* Register the efi directory at /sys/firmware/efi */
  1871. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1872. if (!efi_kobj) {
  1873. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1874. return -ENOMEM;
  1875. }
  1876. error = generic_ops_register();
  1877. if (error)
  1878. goto err_put;
  1879. /* Don't forget the systab entry */
  1880. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1881. if (error) {
  1882. printk(KERN_ERR
  1883. "efivars: Sysfs attribute export failed with error %d.\n",
  1884. error);
  1885. goto err_unregister;
  1886. }
  1887. return 0;
  1888. err_unregister:
  1889. generic_ops_unregister();
  1890. err_put:
  1891. kobject_put(efi_kobj);
  1892. return error;
  1893. }
  1894. static void __exit
  1895. efivars_exit(void)
  1896. {
  1897. cancel_work_sync(&efivar_work);
  1898. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  1899. generic_ops_unregister();
  1900. kobject_put(efi_kobj);
  1901. }
  1902. }
  1903. module_init(efivars_init);
  1904. module_exit(efivars_exit);