main.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * main.c - Multi purpose firmware loading support
  4. *
  5. * Copyright (c) 2003 Manuel Estrada Sainz
  6. *
  7. * Please see Documentation/driver-api/firmware/ for more information.
  8. *
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/capability.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel_read_file.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/initrd.h>
  17. #include <linux/timer.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/bitops.h>
  21. #include <linux/mutex.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/highmem.h>
  24. #include <linux/firmware.h>
  25. #include <linux/slab.h>
  26. #include <linux/sched.h>
  27. #include <linux/file.h>
  28. #include <linux/list.h>
  29. #include <linux/fs.h>
  30. #include <linux/async.h>
  31. #include <linux/pm.h>
  32. #include <linux/suspend.h>
  33. #include <linux/syscore_ops.h>
  34. #include <linux/reboot.h>
  35. #include <linux/security.h>
  36. #include <linux/zstd.h>
  37. #include <linux/xz.h>
  38. #include <generated/utsrelease.h>
  39. #include "../base.h"
  40. #include "firmware.h"
  41. #include "fallback.h"
  42. MODULE_AUTHOR("Manuel Estrada Sainz");
  43. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  44. MODULE_LICENSE("GPL");
  45. struct firmware_cache {
  46. /* firmware_buf instance will be added into the below list */
  47. spinlock_t lock;
  48. struct list_head head;
  49. int state;
  50. #ifdef CONFIG_FW_CACHE
  51. /*
  52. * Names of firmware images which have been cached successfully
  53. * will be added into the below list so that device uncache
  54. * helper can trace which firmware images have been cached
  55. * before.
  56. */
  57. spinlock_t name_lock;
  58. struct list_head fw_names;
  59. struct delayed_work work;
  60. struct notifier_block pm_notify;
  61. #endif
  62. };
  63. struct fw_cache_entry {
  64. struct list_head list;
  65. const char *name;
  66. };
  67. struct fw_name_devm {
  68. unsigned long magic;
  69. const char *name;
  70. };
  71. static inline struct fw_priv *to_fw_priv(struct kref *ref)
  72. {
  73. return container_of(ref, struct fw_priv, ref);
  74. }
  75. #define FW_LOADER_NO_CACHE 0
  76. #define FW_LOADER_START_CACHE 1
  77. /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
  78. * guarding for corner cases a global lock should be OK */
  79. DEFINE_MUTEX(fw_lock);
  80. struct firmware_cache fw_cache;
  81. bool fw_load_abort_all;
  82. void fw_state_init(struct fw_priv *fw_priv)
  83. {
  84. struct fw_state *fw_st = &fw_priv->fw_st;
  85. init_completion(&fw_st->completion);
  86. fw_st->status = FW_STATUS_UNKNOWN;
  87. }
  88. static inline int fw_state_wait(struct fw_priv *fw_priv)
  89. {
  90. return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
  91. }
  92. static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv);
  93. static struct fw_priv *__allocate_fw_priv(const char *fw_name,
  94. struct firmware_cache *fwc,
  95. void *dbuf,
  96. size_t size,
  97. size_t offset,
  98. u32 opt_flags)
  99. {
  100. struct fw_priv *fw_priv;
  101. /* For a partial read, the buffer must be preallocated. */
  102. if ((opt_flags & FW_OPT_PARTIAL) && !dbuf)
  103. return NULL;
  104. /* Only partial reads are allowed to use an offset. */
  105. if (offset != 0 && !(opt_flags & FW_OPT_PARTIAL))
  106. return NULL;
  107. fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
  108. if (!fw_priv)
  109. return NULL;
  110. fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
  111. if (!fw_priv->fw_name) {
  112. kfree(fw_priv);
  113. return NULL;
  114. }
  115. kref_init(&fw_priv->ref);
  116. fw_priv->fwc = fwc;
  117. fw_priv->data = dbuf;
  118. fw_priv->allocated_size = size;
  119. fw_priv->offset = offset;
  120. fw_priv->opt_flags = opt_flags;
  121. fw_state_init(fw_priv);
  122. #ifdef CONFIG_FW_LOADER_USER_HELPER
  123. INIT_LIST_HEAD(&fw_priv->pending_list);
  124. #endif
  125. pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
  126. return fw_priv;
  127. }
  128. static struct fw_priv *__lookup_fw_priv(const char *fw_name)
  129. {
  130. struct fw_priv *tmp;
  131. struct firmware_cache *fwc = &fw_cache;
  132. list_for_each_entry(tmp, &fwc->head, list)
  133. if (!strcmp(tmp->fw_name, fw_name))
  134. return tmp;
  135. return NULL;
  136. }
  137. /* Returns 1 for batching firmware requests with the same name */
  138. int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
  139. struct fw_priv **fw_priv, void *dbuf, size_t size,
  140. size_t offset, u32 opt_flags)
  141. {
  142. struct fw_priv *tmp;
  143. spin_lock(&fwc->lock);
  144. /*
  145. * Do not merge requests that are marked to be non-cached or
  146. * are performing partial reads.
  147. */
  148. if (!(opt_flags & (FW_OPT_NOCACHE | FW_OPT_PARTIAL))) {
  149. tmp = __lookup_fw_priv(fw_name);
  150. if (tmp) {
  151. kref_get(&tmp->ref);
  152. spin_unlock(&fwc->lock);
  153. *fw_priv = tmp;
  154. pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
  155. return 1;
  156. }
  157. }
  158. tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size, offset, opt_flags);
  159. if (tmp) {
  160. INIT_LIST_HEAD(&tmp->list);
  161. if (!(opt_flags & FW_OPT_NOCACHE))
  162. list_add(&tmp->list, &fwc->head);
  163. }
  164. spin_unlock(&fwc->lock);
  165. *fw_priv = tmp;
  166. return tmp ? 0 : -ENOMEM;
  167. }
  168. static void __free_fw_priv(struct kref *ref)
  169. __releases(&fwc->lock)
  170. {
  171. struct fw_priv *fw_priv = to_fw_priv(ref);
  172. struct firmware_cache *fwc = fw_priv->fwc;
  173. pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
  174. __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
  175. (unsigned int)fw_priv->size);
  176. list_del(&fw_priv->list);
  177. spin_unlock(&fwc->lock);
  178. if (fw_is_paged_buf(fw_priv))
  179. fw_free_paged_buf(fw_priv);
  180. else if (!fw_priv->allocated_size)
  181. vfree(fw_priv->data);
  182. kfree_const(fw_priv->fw_name);
  183. kfree(fw_priv);
  184. }
  185. void free_fw_priv(struct fw_priv *fw_priv)
  186. {
  187. struct firmware_cache *fwc = fw_priv->fwc;
  188. spin_lock(&fwc->lock);
  189. if (!kref_put(&fw_priv->ref, __free_fw_priv))
  190. spin_unlock(&fwc->lock);
  191. }
  192. #ifdef CONFIG_FW_LOADER_PAGED_BUF
  193. bool fw_is_paged_buf(struct fw_priv *fw_priv)
  194. {
  195. return fw_priv->is_paged_buf;
  196. }
  197. void fw_free_paged_buf(struct fw_priv *fw_priv)
  198. {
  199. int i;
  200. if (!fw_priv->pages)
  201. return;
  202. vunmap(fw_priv->data);
  203. for (i = 0; i < fw_priv->nr_pages; i++)
  204. __free_page(fw_priv->pages[i]);
  205. kvfree(fw_priv->pages);
  206. fw_priv->pages = NULL;
  207. fw_priv->page_array_size = 0;
  208. fw_priv->nr_pages = 0;
  209. fw_priv->data = NULL;
  210. fw_priv->size = 0;
  211. }
  212. int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
  213. {
  214. /* If the array of pages is too small, grow it */
  215. if (fw_priv->page_array_size < pages_needed) {
  216. int new_array_size = max(pages_needed,
  217. fw_priv->page_array_size * 2);
  218. struct page **new_pages;
  219. new_pages = kvmalloc_array(new_array_size, sizeof(void *),
  220. GFP_KERNEL);
  221. if (!new_pages)
  222. return -ENOMEM;
  223. memcpy(new_pages, fw_priv->pages,
  224. fw_priv->page_array_size * sizeof(void *));
  225. memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
  226. (new_array_size - fw_priv->page_array_size));
  227. kvfree(fw_priv->pages);
  228. fw_priv->pages = new_pages;
  229. fw_priv->page_array_size = new_array_size;
  230. }
  231. while (fw_priv->nr_pages < pages_needed) {
  232. fw_priv->pages[fw_priv->nr_pages] =
  233. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  234. if (!fw_priv->pages[fw_priv->nr_pages])
  235. return -ENOMEM;
  236. fw_priv->nr_pages++;
  237. }
  238. return 0;
  239. }
  240. int fw_map_paged_buf(struct fw_priv *fw_priv)
  241. {
  242. /* one pages buffer should be mapped/unmapped only once */
  243. if (!fw_priv->pages)
  244. return 0;
  245. vunmap(fw_priv->data);
  246. fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
  247. PAGE_KERNEL_RO);
  248. if (!fw_priv->data)
  249. return -ENOMEM;
  250. return 0;
  251. }
  252. #endif
  253. /*
  254. * ZSTD-compressed firmware support
  255. */
  256. #ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD
  257. static int fw_decompress_zstd(struct device *dev, struct fw_priv *fw_priv,
  258. size_t in_size, const void *in_buffer)
  259. {
  260. size_t len, out_size, workspace_size;
  261. void *workspace, *out_buf;
  262. zstd_dctx *ctx;
  263. int err;
  264. if (fw_priv->allocated_size) {
  265. out_size = fw_priv->allocated_size;
  266. out_buf = fw_priv->data;
  267. } else {
  268. zstd_frame_header params;
  269. if (zstd_get_frame_header(&params, in_buffer, in_size) ||
  270. params.frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN) {
  271. dev_dbg(dev, "%s: invalid zstd header\n", __func__);
  272. return -EINVAL;
  273. }
  274. out_size = params.frameContentSize;
  275. out_buf = vzalloc(out_size);
  276. if (!out_buf)
  277. return -ENOMEM;
  278. }
  279. workspace_size = zstd_dctx_workspace_bound();
  280. workspace = kvzalloc(workspace_size, GFP_KERNEL);
  281. if (!workspace) {
  282. err = -ENOMEM;
  283. goto error;
  284. }
  285. ctx = zstd_init_dctx(workspace, workspace_size);
  286. if (!ctx) {
  287. dev_dbg(dev, "%s: failed to initialize context\n", __func__);
  288. err = -EINVAL;
  289. goto error;
  290. }
  291. len = zstd_decompress_dctx(ctx, out_buf, out_size, in_buffer, in_size);
  292. if (zstd_is_error(len)) {
  293. dev_dbg(dev, "%s: failed to decompress: %d\n", __func__,
  294. zstd_get_error_code(len));
  295. err = -EINVAL;
  296. goto error;
  297. }
  298. if (!fw_priv->allocated_size)
  299. fw_priv->data = out_buf;
  300. fw_priv->size = len;
  301. err = 0;
  302. error:
  303. kvfree(workspace);
  304. if (err && !fw_priv->allocated_size)
  305. vfree(out_buf);
  306. return err;
  307. }
  308. #endif /* CONFIG_FW_LOADER_COMPRESS_ZSTD */
  309. /*
  310. * XZ-compressed firmware support
  311. */
  312. #ifdef CONFIG_FW_LOADER_COMPRESS_XZ
  313. /* show an error and return the standard error code */
  314. static int fw_decompress_xz_error(struct device *dev, enum xz_ret xz_ret)
  315. {
  316. if (xz_ret != XZ_STREAM_END) {
  317. dev_warn(dev, "xz decompression failed (xz_ret=%d)\n", xz_ret);
  318. return xz_ret == XZ_MEM_ERROR ? -ENOMEM : -EINVAL;
  319. }
  320. return 0;
  321. }
  322. /* single-shot decompression onto the pre-allocated buffer */
  323. static int fw_decompress_xz_single(struct device *dev, struct fw_priv *fw_priv,
  324. size_t in_size, const void *in_buffer)
  325. {
  326. struct xz_dec *xz_dec;
  327. struct xz_buf xz_buf;
  328. enum xz_ret xz_ret;
  329. xz_dec = xz_dec_init(XZ_SINGLE, (u32)-1);
  330. if (!xz_dec)
  331. return -ENOMEM;
  332. xz_buf.in_size = in_size;
  333. xz_buf.in = in_buffer;
  334. xz_buf.in_pos = 0;
  335. xz_buf.out_size = fw_priv->allocated_size;
  336. xz_buf.out = fw_priv->data;
  337. xz_buf.out_pos = 0;
  338. xz_ret = xz_dec_run(xz_dec, &xz_buf);
  339. xz_dec_end(xz_dec);
  340. fw_priv->size = xz_buf.out_pos;
  341. return fw_decompress_xz_error(dev, xz_ret);
  342. }
  343. /* decompression on paged buffer and map it */
  344. static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
  345. size_t in_size, const void *in_buffer)
  346. {
  347. struct xz_dec *xz_dec;
  348. struct xz_buf xz_buf;
  349. enum xz_ret xz_ret;
  350. struct page *page;
  351. int err = 0;
  352. xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
  353. if (!xz_dec)
  354. return -ENOMEM;
  355. xz_buf.in_size = in_size;
  356. xz_buf.in = in_buffer;
  357. xz_buf.in_pos = 0;
  358. fw_priv->is_paged_buf = true;
  359. fw_priv->size = 0;
  360. do {
  361. if (fw_grow_paged_buf(fw_priv, fw_priv->nr_pages + 1)) {
  362. err = -ENOMEM;
  363. goto out;
  364. }
  365. /* decompress onto the new allocated page */
  366. page = fw_priv->pages[fw_priv->nr_pages - 1];
  367. xz_buf.out = kmap_local_page(page);
  368. xz_buf.out_pos = 0;
  369. xz_buf.out_size = PAGE_SIZE;
  370. xz_ret = xz_dec_run(xz_dec, &xz_buf);
  371. kunmap_local(xz_buf.out);
  372. fw_priv->size += xz_buf.out_pos;
  373. /* partial decompression means either end or error */
  374. if (xz_buf.out_pos != PAGE_SIZE)
  375. break;
  376. } while (xz_ret == XZ_OK);
  377. err = fw_decompress_xz_error(dev, xz_ret);
  378. if (!err)
  379. err = fw_map_paged_buf(fw_priv);
  380. out:
  381. xz_dec_end(xz_dec);
  382. return err;
  383. }
  384. static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
  385. size_t in_size, const void *in_buffer)
  386. {
  387. /* if the buffer is pre-allocated, we can perform in single-shot mode */
  388. if (fw_priv->data)
  389. return fw_decompress_xz_single(dev, fw_priv, in_size, in_buffer);
  390. else
  391. return fw_decompress_xz_pages(dev, fw_priv, in_size, in_buffer);
  392. }
  393. #endif /* CONFIG_FW_LOADER_COMPRESS_XZ */
  394. /* direct firmware loading support */
  395. static char fw_path_para[256];
  396. static const char * const fw_path[] = {
  397. fw_path_para,
  398. "/lib/firmware/updates/" UTS_RELEASE,
  399. "/lib/firmware/updates",
  400. "/lib/firmware/" UTS_RELEASE,
  401. "/lib/firmware"
  402. };
  403. /*
  404. * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
  405. * from kernel command line because firmware_class is generally built in
  406. * kernel instead of module.
  407. */
  408. module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
  409. MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
  410. static int
  411. fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
  412. const char *suffix,
  413. int (*decompress)(struct device *dev,
  414. struct fw_priv *fw_priv,
  415. size_t in_size,
  416. const void *in_buffer))
  417. {
  418. size_t size;
  419. int i, len, maxlen = 0;
  420. int rc = -ENOENT;
  421. char *path, *nt = NULL;
  422. size_t msize = INT_MAX;
  423. void *buffer = NULL;
  424. /* Already populated data member means we're loading into a buffer */
  425. if (!decompress && fw_priv->data) {
  426. buffer = fw_priv->data;
  427. msize = fw_priv->allocated_size;
  428. }
  429. path = __getname();
  430. if (!path)
  431. return -ENOMEM;
  432. wait_for_initramfs();
  433. for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
  434. size_t file_size = 0;
  435. size_t *file_size_ptr = NULL;
  436. /* skip the unset customized path */
  437. if (!fw_path[i][0])
  438. continue;
  439. /* strip off \n from customized path */
  440. maxlen = strlen(fw_path[i]);
  441. if (i == 0) {
  442. nt = strchr(fw_path[i], '\n');
  443. if (nt)
  444. maxlen = nt - fw_path[i];
  445. }
  446. len = snprintf(path, PATH_MAX, "%.*s/%s%s",
  447. maxlen, fw_path[i],
  448. fw_priv->fw_name, suffix);
  449. if (len >= PATH_MAX) {
  450. rc = -ENAMETOOLONG;
  451. break;
  452. }
  453. fw_priv->size = 0;
  454. /*
  455. * The total file size is only examined when doing a partial
  456. * read; the "full read" case needs to fail if the whole
  457. * firmware was not completely loaded.
  458. */
  459. if ((fw_priv->opt_flags & FW_OPT_PARTIAL) && buffer)
  460. file_size_ptr = &file_size;
  461. /* load firmware files from the mount namespace of init */
  462. rc = kernel_read_file_from_path_initns(path, fw_priv->offset,
  463. &buffer, msize,
  464. file_size_ptr,
  465. READING_FIRMWARE);
  466. if (rc < 0) {
  467. if (!(fw_priv->opt_flags & FW_OPT_NO_WARN)) {
  468. if (rc != -ENOENT)
  469. dev_warn(device,
  470. "loading %s failed with error %d\n",
  471. path, rc);
  472. else
  473. dev_dbg(device,
  474. "loading %s failed for no such file or directory.\n",
  475. path);
  476. }
  477. continue;
  478. }
  479. size = rc;
  480. rc = 0;
  481. dev_dbg(device, "Loading firmware from %s\n", path);
  482. if (decompress) {
  483. dev_dbg(device, "f/w decompressing %s\n",
  484. fw_priv->fw_name);
  485. rc = decompress(device, fw_priv, size, buffer);
  486. /* discard the superfluous original content */
  487. vfree(buffer);
  488. buffer = NULL;
  489. if (rc) {
  490. fw_free_paged_buf(fw_priv);
  491. continue;
  492. }
  493. } else {
  494. dev_dbg(device, "direct-loading %s\n",
  495. fw_priv->fw_name);
  496. if (!fw_priv->data)
  497. fw_priv->data = buffer;
  498. fw_priv->size = size;
  499. }
  500. fw_state_done(fw_priv);
  501. break;
  502. }
  503. __putname(path);
  504. return rc;
  505. }
  506. /* firmware holds the ownership of pages */
  507. static void firmware_free_data(const struct firmware *fw)
  508. {
  509. /* Loaded directly? */
  510. if (!fw->priv) {
  511. vfree(fw->data);
  512. return;
  513. }
  514. free_fw_priv(fw->priv);
  515. }
  516. /* store the pages buffer info firmware from buf */
  517. static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
  518. {
  519. fw->priv = fw_priv;
  520. fw->size = fw_priv->size;
  521. fw->data = fw_priv->data;
  522. pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
  523. __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
  524. (unsigned int)fw_priv->size);
  525. }
  526. #ifdef CONFIG_FW_CACHE
  527. static void fw_name_devm_release(struct device *dev, void *res)
  528. {
  529. struct fw_name_devm *fwn = res;
  530. if (fwn->magic == (unsigned long)&fw_cache)
  531. pr_debug("%s: fw_name-%s devm-%p released\n",
  532. __func__, fwn->name, res);
  533. kfree_const(fwn->name);
  534. }
  535. static int fw_devm_match(struct device *dev, void *res,
  536. void *match_data)
  537. {
  538. struct fw_name_devm *fwn = res;
  539. return (fwn->magic == (unsigned long)&fw_cache) &&
  540. !strcmp(fwn->name, match_data);
  541. }
  542. static struct fw_name_devm *fw_find_devm_name(struct device *dev,
  543. const char *name)
  544. {
  545. struct fw_name_devm *fwn;
  546. fwn = devres_find(dev, fw_name_devm_release,
  547. fw_devm_match, (void *)name);
  548. return fwn;
  549. }
  550. static bool fw_cache_is_setup(struct device *dev, const char *name)
  551. {
  552. struct fw_name_devm *fwn;
  553. fwn = fw_find_devm_name(dev, name);
  554. if (fwn)
  555. return true;
  556. return false;
  557. }
  558. /* add firmware name into devres list */
  559. static int fw_add_devm_name(struct device *dev, const char *name)
  560. {
  561. struct fw_name_devm *fwn;
  562. if (fw_cache_is_setup(dev, name))
  563. return 0;
  564. fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
  565. GFP_KERNEL);
  566. if (!fwn)
  567. return -ENOMEM;
  568. fwn->name = kstrdup_const(name, GFP_KERNEL);
  569. if (!fwn->name) {
  570. devres_free(fwn);
  571. return -ENOMEM;
  572. }
  573. fwn->magic = (unsigned long)&fw_cache;
  574. devres_add(dev, fwn);
  575. return 0;
  576. }
  577. #else
  578. static bool fw_cache_is_setup(struct device *dev, const char *name)
  579. {
  580. return false;
  581. }
  582. static int fw_add_devm_name(struct device *dev, const char *name)
  583. {
  584. return 0;
  585. }
  586. #endif
  587. int assign_fw(struct firmware *fw, struct device *device)
  588. {
  589. struct fw_priv *fw_priv = fw->priv;
  590. int ret;
  591. mutex_lock(&fw_lock);
  592. if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
  593. mutex_unlock(&fw_lock);
  594. return -ENOENT;
  595. }
  596. /*
  597. * add firmware name into devres list so that we can auto cache
  598. * and uncache firmware for device.
  599. *
  600. * device may has been deleted already, but the problem
  601. * should be fixed in devres or driver core.
  602. */
  603. /* don't cache firmware handled without uevent */
  604. if (device && (fw_priv->opt_flags & FW_OPT_UEVENT) &&
  605. !(fw_priv->opt_flags & FW_OPT_NOCACHE)) {
  606. ret = fw_add_devm_name(device, fw_priv->fw_name);
  607. if (ret) {
  608. mutex_unlock(&fw_lock);
  609. return ret;
  610. }
  611. }
  612. /*
  613. * After caching firmware image is started, let it piggyback
  614. * on request firmware.
  615. */
  616. if (!(fw_priv->opt_flags & FW_OPT_NOCACHE) &&
  617. fw_priv->fwc->state == FW_LOADER_START_CACHE)
  618. fw_cache_piggyback_on_request(fw_priv);
  619. /* pass the pages buffer to driver at the last minute */
  620. fw_set_page_data(fw_priv, fw);
  621. mutex_unlock(&fw_lock);
  622. return 0;
  623. }
  624. /* prepare firmware and firmware_buf structs;
  625. * return 0 if a firmware is already assigned, 1 if need to load one,
  626. * or a negative error code
  627. */
  628. static int
  629. _request_firmware_prepare(struct firmware **firmware_p, const char *name,
  630. struct device *device, void *dbuf, size_t size,
  631. size_t offset, u32 opt_flags)
  632. {
  633. struct firmware *firmware;
  634. struct fw_priv *fw_priv;
  635. int ret;
  636. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  637. if (!firmware) {
  638. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  639. __func__);
  640. return -ENOMEM;
  641. }
  642. if (firmware_request_builtin_buf(firmware, name, dbuf, size)) {
  643. dev_dbg(device, "using built-in %s\n", name);
  644. return 0; /* assigned */
  645. }
  646. ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
  647. offset, opt_flags);
  648. /*
  649. * bind with 'priv' now to avoid warning in failure path
  650. * of requesting firmware.
  651. */
  652. firmware->priv = fw_priv;
  653. if (ret > 0) {
  654. ret = fw_state_wait(fw_priv);
  655. if (!ret) {
  656. fw_set_page_data(fw_priv, firmware);
  657. return 0; /* assigned */
  658. }
  659. }
  660. if (ret < 0)
  661. return ret;
  662. return 1; /* need to load */
  663. }
  664. /*
  665. * Batched requests need only one wake, we need to do this step last due to the
  666. * fallback mechanism. The buf is protected with kref_get(), and it won't be
  667. * released until the last user calls release_firmware().
  668. *
  669. * Failed batched requests are possible as well, in such cases we just share
  670. * the struct fw_priv and won't release it until all requests are woken
  671. * and have gone through this same path.
  672. */
  673. static void fw_abort_batch_reqs(struct firmware *fw)
  674. {
  675. struct fw_priv *fw_priv;
  676. /* Loaded directly? */
  677. if (!fw || !fw->priv)
  678. return;
  679. fw_priv = fw->priv;
  680. mutex_lock(&fw_lock);
  681. if (!fw_state_is_aborted(fw_priv))
  682. fw_state_aborted(fw_priv);
  683. mutex_unlock(&fw_lock);
  684. }
  685. #if defined(CONFIG_FW_LOADER_DEBUG)
  686. #include <crypto/hash.h>
  687. #include <crypto/sha2.h>
  688. static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
  689. {
  690. struct shash_desc *shash;
  691. struct crypto_shash *alg;
  692. u8 *sha256buf;
  693. char *outbuf;
  694. alg = crypto_alloc_shash("sha256", 0, 0);
  695. if (IS_ERR(alg))
  696. return;
  697. sha256buf = kmalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  698. outbuf = kmalloc(SHA256_BLOCK_SIZE + 1, GFP_KERNEL);
  699. shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(alg), GFP_KERNEL);
  700. if (!sha256buf || !outbuf || !shash)
  701. goto out_free;
  702. shash->tfm = alg;
  703. if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
  704. goto out_free;
  705. for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
  706. sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
  707. outbuf[SHA256_BLOCK_SIZE] = 0;
  708. dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
  709. out_free:
  710. kfree(shash);
  711. kfree(outbuf);
  712. kfree(sha256buf);
  713. crypto_free_shash(alg);
  714. }
  715. #else
  716. static void fw_log_firmware_info(const struct firmware *fw, const char *name,
  717. struct device *device)
  718. {}
  719. #endif
  720. /*
  721. * Reject firmware file names with ".." path components.
  722. * There are drivers that construct firmware file names from device-supplied
  723. * strings, and we don't want some device to be able to tell us "I would like to
  724. * be sent my firmware from ../../../etc/shadow, please".
  725. *
  726. * Search for ".." surrounded by either '/' or start/end of string.
  727. *
  728. * This intentionally only looks at the firmware name, not at the firmware base
  729. * directory or at symlink contents.
  730. */
  731. static bool name_contains_dotdot(const char *name)
  732. {
  733. size_t name_len = strlen(name);
  734. return strcmp(name, "..") == 0 || strncmp(name, "../", 3) == 0 ||
  735. strstr(name, "/../") != NULL ||
  736. (name_len >= 3 && strcmp(name+name_len-3, "/..") == 0);
  737. }
  738. /* called from request_firmware() and request_firmware_work_func() */
  739. static int
  740. _request_firmware(const struct firmware **firmware_p, const char *name,
  741. struct device *device, void *buf, size_t size,
  742. size_t offset, u32 opt_flags)
  743. {
  744. struct firmware *fw = NULL;
  745. struct cred *kern_cred = NULL;
  746. const struct cred *old_cred;
  747. bool nondirect = false;
  748. int ret;
  749. if (!firmware_p)
  750. return -EINVAL;
  751. if (!name || name[0] == '\0') {
  752. ret = -EINVAL;
  753. goto out;
  754. }
  755. if (name_contains_dotdot(name)) {
  756. dev_warn(device,
  757. "Firmware load for '%s' refused, path contains '..' component\n",
  758. name);
  759. ret = -EINVAL;
  760. goto out;
  761. }
  762. ret = _request_firmware_prepare(&fw, name, device, buf, size,
  763. offset, opt_flags);
  764. if (ret <= 0) /* error or already assigned */
  765. goto out;
  766. /*
  767. * We are about to try to access the firmware file. Because we may have been
  768. * called by a driver when serving an unrelated request from userland, we use
  769. * the kernel credentials to read the file.
  770. */
  771. kern_cred = prepare_kernel_cred(&init_task);
  772. if (!kern_cred) {
  773. ret = -ENOMEM;
  774. goto out;
  775. }
  776. old_cred = override_creds(kern_cred);
  777. ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
  778. /* Only full reads can support decompression, platform, and sysfs. */
  779. if (!(opt_flags & FW_OPT_PARTIAL))
  780. nondirect = true;
  781. #ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD
  782. if (ret == -ENOENT && nondirect)
  783. ret = fw_get_filesystem_firmware(device, fw->priv, ".zst",
  784. fw_decompress_zstd);
  785. #endif
  786. #ifdef CONFIG_FW_LOADER_COMPRESS_XZ
  787. if (ret == -ENOENT && nondirect)
  788. ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
  789. fw_decompress_xz);
  790. #endif
  791. if (ret == -ENOENT && nondirect)
  792. ret = firmware_fallback_platform(fw->priv);
  793. if (ret) {
  794. if (!(opt_flags & FW_OPT_NO_WARN))
  795. dev_warn(device,
  796. "Direct firmware load for %s failed with error %d\n",
  797. name, ret);
  798. if (nondirect)
  799. ret = firmware_fallback_sysfs(fw, name, device,
  800. opt_flags, ret);
  801. } else
  802. ret = assign_fw(fw, device);
  803. revert_creds(old_cred);
  804. put_cred(kern_cred);
  805. out:
  806. if (ret < 0) {
  807. fw_abort_batch_reqs(fw);
  808. release_firmware(fw);
  809. fw = NULL;
  810. } else {
  811. fw_log_firmware_info(fw, name, device);
  812. }
  813. *firmware_p = fw;
  814. return ret;
  815. }
  816. /**
  817. * request_firmware() - send firmware request and wait for it
  818. * @firmware_p: pointer to firmware image
  819. * @name: name of firmware file
  820. * @device: device for which firmware is being loaded
  821. *
  822. * @firmware_p will be used to return a firmware image by the name
  823. * of @name for device @device.
  824. *
  825. * Should be called from user context where sleeping is allowed.
  826. *
  827. * @name will be used as $FIRMWARE in the uevent environment and
  828. * should be distinctive enough not to be confused with any other
  829. * firmware image for this or any other device.
  830. * It must not contain any ".." path components - "foo/bar..bin" is
  831. * allowed, but "foo/../bar.bin" is not.
  832. *
  833. * Caller must hold the reference count of @device.
  834. *
  835. * The function can be called safely inside device's suspend and
  836. * resume callback.
  837. **/
  838. int
  839. request_firmware(const struct firmware **firmware_p, const char *name,
  840. struct device *device)
  841. {
  842. int ret;
  843. /* Need to pin this module until return */
  844. __module_get(THIS_MODULE);
  845. ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
  846. FW_OPT_UEVENT);
  847. module_put(THIS_MODULE);
  848. return ret;
  849. }
  850. EXPORT_SYMBOL(request_firmware);
  851. /**
  852. * firmware_request_nowarn() - request for an optional fw module
  853. * @firmware: pointer to firmware image
  854. * @name: name of firmware file
  855. * @device: device for which firmware is being loaded
  856. *
  857. * This function is similar in behaviour to request_firmware(), except it
  858. * doesn't produce warning messages when the file is not found. The sysfs
  859. * fallback mechanism is enabled if direct filesystem lookup fails. However,
  860. * failures to find the firmware file with it are still suppressed. It is
  861. * therefore up to the driver to check for the return value of this call and to
  862. * decide when to inform the users of errors.
  863. **/
  864. int firmware_request_nowarn(const struct firmware **firmware, const char *name,
  865. struct device *device)
  866. {
  867. int ret;
  868. /* Need to pin this module until return */
  869. __module_get(THIS_MODULE);
  870. ret = _request_firmware(firmware, name, device, NULL, 0, 0,
  871. FW_OPT_UEVENT | FW_OPT_NO_WARN);
  872. module_put(THIS_MODULE);
  873. return ret;
  874. }
  875. EXPORT_SYMBOL_GPL(firmware_request_nowarn);
  876. /**
  877. * request_firmware_direct() - load firmware directly without usermode helper
  878. * @firmware_p: pointer to firmware image
  879. * @name: name of firmware file
  880. * @device: device for which firmware is being loaded
  881. *
  882. * This function works pretty much like request_firmware(), but this doesn't
  883. * fall back to usermode helper even if the firmware couldn't be loaded
  884. * directly from fs. Hence it's useful for loading optional firmwares, which
  885. * aren't always present, without extra long timeouts of udev.
  886. **/
  887. int request_firmware_direct(const struct firmware **firmware_p,
  888. const char *name, struct device *device)
  889. {
  890. int ret;
  891. __module_get(THIS_MODULE);
  892. ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
  893. FW_OPT_UEVENT | FW_OPT_NO_WARN |
  894. FW_OPT_NOFALLBACK_SYSFS);
  895. module_put(THIS_MODULE);
  896. return ret;
  897. }
  898. EXPORT_SYMBOL_GPL(request_firmware_direct);
  899. /**
  900. * firmware_request_platform() - request firmware with platform-fw fallback
  901. * @firmware: pointer to firmware image
  902. * @name: name of firmware file
  903. * @device: device for which firmware is being loaded
  904. *
  905. * This function is similar in behaviour to request_firmware, except that if
  906. * direct filesystem lookup fails, it will fallback to looking for a copy of the
  907. * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
  908. **/
  909. int firmware_request_platform(const struct firmware **firmware,
  910. const char *name, struct device *device)
  911. {
  912. int ret;
  913. /* Need to pin this module until return */
  914. __module_get(THIS_MODULE);
  915. ret = _request_firmware(firmware, name, device, NULL, 0, 0,
  916. FW_OPT_UEVENT | FW_OPT_FALLBACK_PLATFORM);
  917. module_put(THIS_MODULE);
  918. return ret;
  919. }
  920. EXPORT_SYMBOL_GPL(firmware_request_platform);
  921. /**
  922. * firmware_request_cache() - cache firmware for suspend so resume can use it
  923. * @name: name of firmware file
  924. * @device: device for which firmware should be cached for
  925. *
  926. * There are some devices with an optimization that enables the device to not
  927. * require loading firmware on system reboot. This optimization may still
  928. * require the firmware present on resume from suspend. This routine can be
  929. * used to ensure the firmware is present on resume from suspend in these
  930. * situations. This helper is not compatible with drivers which use
  931. * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
  932. **/
  933. int firmware_request_cache(struct device *device, const char *name)
  934. {
  935. int ret;
  936. mutex_lock(&fw_lock);
  937. ret = fw_add_devm_name(device, name);
  938. mutex_unlock(&fw_lock);
  939. return ret;
  940. }
  941. EXPORT_SYMBOL_GPL(firmware_request_cache);
  942. /**
  943. * request_firmware_into_buf() - load firmware into a previously allocated buffer
  944. * @firmware_p: pointer to firmware image
  945. * @name: name of firmware file
  946. * @device: device for which firmware is being loaded and DMA region allocated
  947. * @buf: address of buffer to load firmware into
  948. * @size: size of buffer
  949. *
  950. * This function works pretty much like request_firmware(), but it doesn't
  951. * allocate a buffer to hold the firmware data. Instead, the firmware
  952. * is loaded directly into the buffer pointed to by @buf and the @firmware_p
  953. * data member is pointed at @buf.
  954. *
  955. * This function doesn't cache firmware either.
  956. */
  957. int
  958. request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
  959. struct device *device, void *buf, size_t size)
  960. {
  961. int ret;
  962. if (fw_cache_is_setup(device, name))
  963. return -EOPNOTSUPP;
  964. __module_get(THIS_MODULE);
  965. ret = _request_firmware(firmware_p, name, device, buf, size, 0,
  966. FW_OPT_UEVENT | FW_OPT_NOCACHE);
  967. module_put(THIS_MODULE);
  968. return ret;
  969. }
  970. EXPORT_SYMBOL(request_firmware_into_buf);
  971. /**
  972. * request_partial_firmware_into_buf() - load partial firmware into a previously allocated buffer
  973. * @firmware_p: pointer to firmware image
  974. * @name: name of firmware file
  975. * @device: device for which firmware is being loaded and DMA region allocated
  976. * @buf: address of buffer to load firmware into
  977. * @size: size of buffer
  978. * @offset: offset into file to read
  979. *
  980. * This function works pretty much like request_firmware_into_buf except
  981. * it allows a partial read of the file.
  982. */
  983. int
  984. request_partial_firmware_into_buf(const struct firmware **firmware_p,
  985. const char *name, struct device *device,
  986. void *buf, size_t size, size_t offset)
  987. {
  988. int ret;
  989. if (fw_cache_is_setup(device, name))
  990. return -EOPNOTSUPP;
  991. __module_get(THIS_MODULE);
  992. ret = _request_firmware(firmware_p, name, device, buf, size, offset,
  993. FW_OPT_UEVENT | FW_OPT_NOCACHE |
  994. FW_OPT_PARTIAL);
  995. module_put(THIS_MODULE);
  996. return ret;
  997. }
  998. EXPORT_SYMBOL(request_partial_firmware_into_buf);
  999. /**
  1000. * release_firmware() - release the resource associated with a firmware image
  1001. * @fw: firmware resource to release
  1002. **/
  1003. void release_firmware(const struct firmware *fw)
  1004. {
  1005. if (fw) {
  1006. if (!firmware_is_builtin(fw))
  1007. firmware_free_data(fw);
  1008. kfree(fw);
  1009. }
  1010. }
  1011. EXPORT_SYMBOL(release_firmware);
  1012. /* Async support */
  1013. struct firmware_work {
  1014. struct work_struct work;
  1015. struct module *module;
  1016. const char *name;
  1017. struct device *device;
  1018. void *context;
  1019. void (*cont)(const struct firmware *fw, void *context);
  1020. u32 opt_flags;
  1021. };
  1022. static void request_firmware_work_func(struct work_struct *work)
  1023. {
  1024. struct firmware_work *fw_work;
  1025. const struct firmware *fw;
  1026. fw_work = container_of(work, struct firmware_work, work);
  1027. _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0, 0,
  1028. fw_work->opt_flags);
  1029. fw_work->cont(fw, fw_work->context);
  1030. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  1031. module_put(fw_work->module);
  1032. kfree_const(fw_work->name);
  1033. kfree(fw_work);
  1034. }
  1035. static int _request_firmware_nowait(
  1036. struct module *module, bool uevent,
  1037. const char *name, struct device *device, gfp_t gfp, void *context,
  1038. void (*cont)(const struct firmware *fw, void *context), bool nowarn)
  1039. {
  1040. struct firmware_work *fw_work;
  1041. fw_work = kzalloc(sizeof(struct firmware_work), gfp);
  1042. if (!fw_work)
  1043. return -ENOMEM;
  1044. fw_work->module = module;
  1045. fw_work->name = kstrdup_const(name, gfp);
  1046. if (!fw_work->name) {
  1047. kfree(fw_work);
  1048. return -ENOMEM;
  1049. }
  1050. fw_work->device = device;
  1051. fw_work->context = context;
  1052. fw_work->cont = cont;
  1053. fw_work->opt_flags = FW_OPT_NOWAIT |
  1054. (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER) |
  1055. (nowarn ? FW_OPT_NO_WARN : 0);
  1056. if (!uevent && fw_cache_is_setup(device, name)) {
  1057. kfree_const(fw_work->name);
  1058. kfree(fw_work);
  1059. return -EOPNOTSUPP;
  1060. }
  1061. if (!try_module_get(module)) {
  1062. kfree_const(fw_work->name);
  1063. kfree(fw_work);
  1064. return -EFAULT;
  1065. }
  1066. get_device(fw_work->device);
  1067. INIT_WORK(&fw_work->work, request_firmware_work_func);
  1068. schedule_work(&fw_work->work);
  1069. return 0;
  1070. }
  1071. /**
  1072. * request_firmware_nowait() - asynchronous version of request_firmware
  1073. * @module: module requesting the firmware
  1074. * @uevent: sends uevent to copy the firmware image if this flag
  1075. * is non-zero else the firmware copy must be done manually.
  1076. * @name: name of firmware file
  1077. * @device: device for which firmware is being loaded
  1078. * @gfp: allocation flags
  1079. * @context: will be passed over to @cont, and
  1080. * @fw may be %NULL if firmware request fails.
  1081. * @cont: function will be called asynchronously when the firmware
  1082. * request is over.
  1083. *
  1084. * Caller must hold the reference count of @device.
  1085. *
  1086. * Asynchronous variant of request_firmware() for user contexts:
  1087. * - sleep for as small periods as possible since it may
  1088. * increase kernel boot time of built-in device drivers
  1089. * requesting firmware in their ->probe() methods, if
  1090. * @gfp is GFP_KERNEL.
  1091. *
  1092. * - can't sleep at all if @gfp is GFP_ATOMIC.
  1093. **/
  1094. int request_firmware_nowait(
  1095. struct module *module, bool uevent,
  1096. const char *name, struct device *device, gfp_t gfp, void *context,
  1097. void (*cont)(const struct firmware *fw, void *context))
  1098. {
  1099. return _request_firmware_nowait(module, uevent, name, device, gfp,
  1100. context, cont, false);
  1101. }
  1102. EXPORT_SYMBOL(request_firmware_nowait);
  1103. /**
  1104. * firmware_request_nowait_nowarn() - async version of request_firmware_nowarn
  1105. * @module: module requesting the firmware
  1106. * @name: name of firmware file
  1107. * @device: device for which firmware is being loaded
  1108. * @gfp: allocation flags
  1109. * @context: will be passed over to @cont, and
  1110. * @fw may be %NULL if firmware request fails.
  1111. * @cont: function will be called asynchronously when the firmware
  1112. * request is over.
  1113. *
  1114. * Similar in function to request_firmware_nowait(), but doesn't print a warning
  1115. * when the firmware file could not be found and always sends a uevent to copy
  1116. * the firmware image.
  1117. */
  1118. int firmware_request_nowait_nowarn(
  1119. struct module *module, const char *name,
  1120. struct device *device, gfp_t gfp, void *context,
  1121. void (*cont)(const struct firmware *fw, void *context))
  1122. {
  1123. return _request_firmware_nowait(module, FW_ACTION_UEVENT, name, device,
  1124. gfp, context, cont, true);
  1125. }
  1126. EXPORT_SYMBOL_GPL(firmware_request_nowait_nowarn);
  1127. #ifdef CONFIG_FW_CACHE
  1128. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  1129. /**
  1130. * cache_firmware() - cache one firmware image in kernel memory space
  1131. * @fw_name: the firmware image name
  1132. *
  1133. * Cache firmware in kernel memory so that drivers can use it when
  1134. * system isn't ready for them to request firmware image from userspace.
  1135. * Once it returns successfully, driver can use request_firmware or its
  1136. * nowait version to get the cached firmware without any interacting
  1137. * with userspace
  1138. *
  1139. * Return 0 if the firmware image has been cached successfully
  1140. * Return !0 otherwise
  1141. *
  1142. */
  1143. static int cache_firmware(const char *fw_name)
  1144. {
  1145. int ret;
  1146. const struct firmware *fw;
  1147. pr_debug("%s: %s\n", __func__, fw_name);
  1148. ret = request_firmware(&fw, fw_name, NULL);
  1149. if (!ret)
  1150. kfree(fw);
  1151. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  1152. return ret;
  1153. }
  1154. static struct fw_priv *lookup_fw_priv(const char *fw_name)
  1155. {
  1156. struct fw_priv *tmp;
  1157. struct firmware_cache *fwc = &fw_cache;
  1158. spin_lock(&fwc->lock);
  1159. tmp = __lookup_fw_priv(fw_name);
  1160. spin_unlock(&fwc->lock);
  1161. return tmp;
  1162. }
  1163. /**
  1164. * uncache_firmware() - remove one cached firmware image
  1165. * @fw_name: the firmware image name
  1166. *
  1167. * Uncache one firmware image which has been cached successfully
  1168. * before.
  1169. *
  1170. * Return 0 if the firmware cache has been removed successfully
  1171. * Return !0 otherwise
  1172. *
  1173. */
  1174. static int uncache_firmware(const char *fw_name)
  1175. {
  1176. struct fw_priv *fw_priv;
  1177. struct firmware fw;
  1178. pr_debug("%s: %s\n", __func__, fw_name);
  1179. if (firmware_request_builtin(&fw, fw_name))
  1180. return 0;
  1181. fw_priv = lookup_fw_priv(fw_name);
  1182. if (fw_priv) {
  1183. free_fw_priv(fw_priv);
  1184. return 0;
  1185. }
  1186. return -EINVAL;
  1187. }
  1188. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  1189. {
  1190. struct fw_cache_entry *fce;
  1191. fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
  1192. if (!fce)
  1193. goto exit;
  1194. fce->name = kstrdup_const(name, GFP_ATOMIC);
  1195. if (!fce->name) {
  1196. kfree(fce);
  1197. fce = NULL;
  1198. goto exit;
  1199. }
  1200. exit:
  1201. return fce;
  1202. }
  1203. static int __fw_entry_found(const char *name)
  1204. {
  1205. struct firmware_cache *fwc = &fw_cache;
  1206. struct fw_cache_entry *fce;
  1207. list_for_each_entry(fce, &fwc->fw_names, list) {
  1208. if (!strcmp(fce->name, name))
  1209. return 1;
  1210. }
  1211. return 0;
  1212. }
  1213. static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
  1214. {
  1215. const char *name = fw_priv->fw_name;
  1216. struct firmware_cache *fwc = fw_priv->fwc;
  1217. struct fw_cache_entry *fce;
  1218. spin_lock(&fwc->name_lock);
  1219. if (__fw_entry_found(name))
  1220. goto found;
  1221. fce = alloc_fw_cache_entry(name);
  1222. if (fce) {
  1223. list_add(&fce->list, &fwc->fw_names);
  1224. kref_get(&fw_priv->ref);
  1225. pr_debug("%s: fw: %s\n", __func__, name);
  1226. }
  1227. found:
  1228. spin_unlock(&fwc->name_lock);
  1229. }
  1230. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  1231. {
  1232. kfree_const(fce->name);
  1233. kfree(fce);
  1234. }
  1235. static void __async_dev_cache_fw_image(void *fw_entry,
  1236. async_cookie_t cookie)
  1237. {
  1238. struct fw_cache_entry *fce = fw_entry;
  1239. struct firmware_cache *fwc = &fw_cache;
  1240. int ret;
  1241. ret = cache_firmware(fce->name);
  1242. if (ret) {
  1243. spin_lock(&fwc->name_lock);
  1244. list_del(&fce->list);
  1245. spin_unlock(&fwc->name_lock);
  1246. free_fw_cache_entry(fce);
  1247. }
  1248. }
  1249. /* called with dev->devres_lock held */
  1250. static void dev_create_fw_entry(struct device *dev, void *res,
  1251. void *data)
  1252. {
  1253. struct fw_name_devm *fwn = res;
  1254. const char *fw_name = fwn->name;
  1255. struct list_head *head = data;
  1256. struct fw_cache_entry *fce;
  1257. fce = alloc_fw_cache_entry(fw_name);
  1258. if (fce)
  1259. list_add(&fce->list, head);
  1260. }
  1261. static int devm_name_match(struct device *dev, void *res,
  1262. void *match_data)
  1263. {
  1264. struct fw_name_devm *fwn = res;
  1265. return (fwn->magic == (unsigned long)match_data);
  1266. }
  1267. static void dev_cache_fw_image(struct device *dev, void *data)
  1268. {
  1269. LIST_HEAD(todo);
  1270. struct fw_cache_entry *fce;
  1271. struct fw_cache_entry *fce_next;
  1272. struct firmware_cache *fwc = &fw_cache;
  1273. devres_for_each_res(dev, fw_name_devm_release,
  1274. devm_name_match, &fw_cache,
  1275. dev_create_fw_entry, &todo);
  1276. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  1277. list_del(&fce->list);
  1278. spin_lock(&fwc->name_lock);
  1279. /* only one cache entry for one firmware */
  1280. if (!__fw_entry_found(fce->name)) {
  1281. list_add(&fce->list, &fwc->fw_names);
  1282. } else {
  1283. free_fw_cache_entry(fce);
  1284. fce = NULL;
  1285. }
  1286. spin_unlock(&fwc->name_lock);
  1287. if (fce)
  1288. async_schedule_domain(__async_dev_cache_fw_image,
  1289. (void *)fce,
  1290. &fw_cache_domain);
  1291. }
  1292. }
  1293. static void __device_uncache_fw_images(void)
  1294. {
  1295. struct firmware_cache *fwc = &fw_cache;
  1296. struct fw_cache_entry *fce;
  1297. spin_lock(&fwc->name_lock);
  1298. while (!list_empty(&fwc->fw_names)) {
  1299. fce = list_entry(fwc->fw_names.next,
  1300. struct fw_cache_entry, list);
  1301. list_del(&fce->list);
  1302. spin_unlock(&fwc->name_lock);
  1303. uncache_firmware(fce->name);
  1304. free_fw_cache_entry(fce);
  1305. spin_lock(&fwc->name_lock);
  1306. }
  1307. spin_unlock(&fwc->name_lock);
  1308. }
  1309. /**
  1310. * device_cache_fw_images() - cache devices' firmware
  1311. *
  1312. * If one device called request_firmware or its nowait version
  1313. * successfully before, the firmware names are recored into the
  1314. * device's devres link list, so device_cache_fw_images can call
  1315. * cache_firmware() to cache these firmwares for the device,
  1316. * then the device driver can load its firmwares easily at
  1317. * time when system is not ready to complete loading firmware.
  1318. */
  1319. static void device_cache_fw_images(void)
  1320. {
  1321. struct firmware_cache *fwc = &fw_cache;
  1322. DEFINE_WAIT(wait);
  1323. pr_debug("%s\n", __func__);
  1324. /* cancel uncache work */
  1325. cancel_delayed_work_sync(&fwc->work);
  1326. fw_fallback_set_cache_timeout();
  1327. mutex_lock(&fw_lock);
  1328. fwc->state = FW_LOADER_START_CACHE;
  1329. dpm_for_each_dev(NULL, dev_cache_fw_image);
  1330. mutex_unlock(&fw_lock);
  1331. /* wait for completion of caching firmware for all devices */
  1332. async_synchronize_full_domain(&fw_cache_domain);
  1333. fw_fallback_set_default_timeout();
  1334. }
  1335. /**
  1336. * device_uncache_fw_images() - uncache devices' firmware
  1337. *
  1338. * uncache all firmwares which have been cached successfully
  1339. * by device_uncache_fw_images earlier
  1340. */
  1341. static void device_uncache_fw_images(void)
  1342. {
  1343. pr_debug("%s\n", __func__);
  1344. __device_uncache_fw_images();
  1345. }
  1346. static void device_uncache_fw_images_work(struct work_struct *work)
  1347. {
  1348. device_uncache_fw_images();
  1349. }
  1350. /**
  1351. * device_uncache_fw_images_delay() - uncache devices firmwares
  1352. * @delay: number of milliseconds to delay uncache device firmwares
  1353. *
  1354. * uncache all devices's firmwares which has been cached successfully
  1355. * by device_cache_fw_images after @delay milliseconds.
  1356. */
  1357. static void device_uncache_fw_images_delay(unsigned long delay)
  1358. {
  1359. queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
  1360. msecs_to_jiffies(delay));
  1361. }
  1362. static int fw_pm_notify(struct notifier_block *notify_block,
  1363. unsigned long mode, void *unused)
  1364. {
  1365. switch (mode) {
  1366. case PM_HIBERNATION_PREPARE:
  1367. case PM_SUSPEND_PREPARE:
  1368. case PM_RESTORE_PREPARE:
  1369. /*
  1370. * Here, kill pending fallback requests will only kill
  1371. * non-uevent firmware request to avoid stalling suspend.
  1372. */
  1373. kill_pending_fw_fallback_reqs(false);
  1374. device_cache_fw_images();
  1375. break;
  1376. case PM_POST_SUSPEND:
  1377. case PM_POST_HIBERNATION:
  1378. case PM_POST_RESTORE:
  1379. /*
  1380. * In case that system sleep failed and syscore_suspend is
  1381. * not called.
  1382. */
  1383. mutex_lock(&fw_lock);
  1384. fw_cache.state = FW_LOADER_NO_CACHE;
  1385. mutex_unlock(&fw_lock);
  1386. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  1387. break;
  1388. }
  1389. return 0;
  1390. }
  1391. /* stop caching firmware once syscore_suspend is reached */
  1392. static int fw_suspend(void)
  1393. {
  1394. fw_cache.state = FW_LOADER_NO_CACHE;
  1395. return 0;
  1396. }
  1397. static struct syscore_ops fw_syscore_ops = {
  1398. .suspend = fw_suspend,
  1399. };
  1400. static int __init register_fw_pm_ops(void)
  1401. {
  1402. int ret;
  1403. spin_lock_init(&fw_cache.name_lock);
  1404. INIT_LIST_HEAD(&fw_cache.fw_names);
  1405. INIT_DELAYED_WORK(&fw_cache.work,
  1406. device_uncache_fw_images_work);
  1407. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  1408. ret = register_pm_notifier(&fw_cache.pm_notify);
  1409. if (ret)
  1410. return ret;
  1411. register_syscore_ops(&fw_syscore_ops);
  1412. return ret;
  1413. }
  1414. static inline void unregister_fw_pm_ops(void)
  1415. {
  1416. unregister_syscore_ops(&fw_syscore_ops);
  1417. unregister_pm_notifier(&fw_cache.pm_notify);
  1418. }
  1419. #else
  1420. static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
  1421. {
  1422. }
  1423. static inline int register_fw_pm_ops(void)
  1424. {
  1425. return 0;
  1426. }
  1427. static inline void unregister_fw_pm_ops(void)
  1428. {
  1429. }
  1430. #endif
  1431. static void __init fw_cache_init(void)
  1432. {
  1433. spin_lock_init(&fw_cache.lock);
  1434. INIT_LIST_HEAD(&fw_cache.head);
  1435. fw_cache.state = FW_LOADER_NO_CACHE;
  1436. }
  1437. static int fw_shutdown_notify(struct notifier_block *unused1,
  1438. unsigned long unused2, void *unused3)
  1439. {
  1440. /*
  1441. * Kill all pending fallback requests to avoid both stalling shutdown,
  1442. * and avoid a deadlock with the usermode_lock.
  1443. */
  1444. kill_pending_fw_fallback_reqs(true);
  1445. return NOTIFY_DONE;
  1446. }
  1447. static struct notifier_block fw_shutdown_nb = {
  1448. .notifier_call = fw_shutdown_notify,
  1449. };
  1450. static int __init firmware_class_init(void)
  1451. {
  1452. int ret;
  1453. /* No need to unfold these on exit */
  1454. fw_cache_init();
  1455. ret = register_fw_pm_ops();
  1456. if (ret)
  1457. return ret;
  1458. ret = register_reboot_notifier(&fw_shutdown_nb);
  1459. if (ret)
  1460. goto out;
  1461. return register_sysfs_loader();
  1462. out:
  1463. unregister_fw_pm_ops();
  1464. return ret;
  1465. }
  1466. static void __exit firmware_class_exit(void)
  1467. {
  1468. unregister_fw_pm_ops();
  1469. unregister_reboot_notifier(&fw_shutdown_nb);
  1470. unregister_sysfs_loader();
  1471. }
  1472. fs_initcall(firmware_class_init);
  1473. module_exit(firmware_class_exit);