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