]>
Commit | Line | Data |
---|---|---|
85eaf38c KS |
1 | /* |
2 | * firmware - Kernel firmware loader | |
3 | * | |
4 | * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com> | |
1298001e | 5 | * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org> |
85eaf38c KS |
6 | * |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License as | |
9 | * published by the Free Software Foundation; either version 2 of the | |
10 | * License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details:* | |
16 | */ | |
17 | ||
18 | #include <unistd.h> | |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <stdio.h> | |
22 | #include <getopt.h> | |
23 | #include <errno.h> | |
24 | #include <stdbool.h> | |
25 | #include <sys/utsname.h> | |
26 | #include <sys/stat.h> | |
27 | ||
28 | #include "udev.h" | |
29 | ||
30 | static bool set_loading(struct udev *udev, char *loadpath, const char *state) | |
31 | { | |
912541b0 KS |
32 | FILE *ldfile; |
33 | ||
34 | ldfile = fopen(loadpath, "we"); | |
35 | if (ldfile == NULL) { | |
baa30fbc | 36 | log_error("error: can not open '%s'\n", loadpath); |
912541b0 KS |
37 | return false; |
38 | }; | |
39 | fprintf(ldfile, "%s\n", state); | |
40 | fclose(ldfile); | |
41 | return true; | |
85eaf38c KS |
42 | } |
43 | ||
44 | static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size) | |
45 | { | |
912541b0 KS |
46 | char *buf; |
47 | FILE *fsource = NULL, *ftarget = NULL; | |
48 | bool ret = false; | |
49 | ||
50 | buf = malloc(size); | |
51 | if (buf == NULL) { | |
baa30fbc | 52 | log_error("No memory available to load firmware file"); |
912541b0 KS |
53 | return false; |
54 | } | |
55 | ||
baa30fbc | 56 | log_debug("writing '%s' (%zi) to '%s'\n", source, size, target); |
912541b0 KS |
57 | |
58 | fsource = fopen(source, "re"); | |
59 | if (fsource == NULL) | |
60 | goto exit; | |
61 | ftarget = fopen(target, "we"); | |
62 | if (ftarget == NULL) | |
63 | goto exit; | |
64 | if (fread(buf, size, 1, fsource) != 1) | |
65 | goto exit; | |
66 | if (fwrite(buf, size, 1, ftarget) == 1) | |
67 | ret = true; | |
85eaf38c | 68 | exit: |
912541b0 KS |
69 | if (ftarget != NULL) |
70 | fclose(ftarget); | |
71 | if (fsource != NULL) | |
72 | fclose(fsource); | |
73 | free(buf); | |
74 | return ret; | |
85eaf38c KS |
75 | } |
76 | ||
77 | static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test) | |
78 | { | |
912541b0 KS |
79 | struct udev *udev = udev_device_get_udev(dev); |
80 | static const char *searchpath[] = { FIRMWARE_PATH }; | |
912541b0 KS |
81 | char loadpath[UTIL_PATH_SIZE]; |
82 | char datapath[UTIL_PATH_SIZE]; | |
83 | char fwpath[UTIL_PATH_SIZE]; | |
84 | const char *firmware; | |
b49d9b50 | 85 | FILE *fwfile = NULL; |
912541b0 KS |
86 | struct utsname kernel; |
87 | struct stat statbuf; | |
88 | unsigned int i; | |
89 | int rc = EXIT_SUCCESS; | |
90 | ||
91 | firmware = udev_device_get_property_value(dev, "FIRMWARE"); | |
92 | if (firmware == NULL) { | |
baa30fbc | 93 | log_error("firmware parameter missing\n\n"); |
912541b0 KS |
94 | rc = EXIT_FAILURE; |
95 | goto exit; | |
96 | } | |
97 | ||
98 | /* lookup firmware file */ | |
99 | uname(&kernel); | |
8fef0ff2 | 100 | for (i = 0; i < ELEMENTSOF(searchpath); i++) { |
d5a89d7d | 101 | strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL); |
912541b0 KS |
102 | fwfile = fopen(fwpath, "re"); |
103 | if (fwfile != NULL) | |
104 | break; | |
105 | ||
d5a89d7d | 106 | strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL); |
912541b0 KS |
107 | fwfile = fopen(fwpath, "re"); |
108 | if (fwfile != NULL) | |
109 | break; | |
110 | } | |
111 | ||
d5a89d7d | 112 | strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL); |
912541b0 KS |
113 | |
114 | if (fwfile == NULL) { | |
baa30fbc | 115 | log_debug("did not find firmware file '%s'\n", firmware); |
912541b0 | 116 | rc = EXIT_FAILURE; |
39177382 KS |
117 | /* |
118 | * Do not cancel the request in the initrd, the real root might have | |
119 | * the firmware file and the 'coldplug' run in the real root will find | |
120 | * this pending request and fulfill or cancel it. | |
121 | * */ | |
122 | if (!in_initrd()) | |
123 | set_loading(udev, loadpath, "-1"); | |
912541b0 KS |
124 | goto exit; |
125 | } | |
126 | ||
127 | if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) { | |
5bb633f1 UTL |
128 | if (!in_initrd()) |
129 | set_loading(udev, loadpath, "-1"); | |
912541b0 KS |
130 | rc = EXIT_FAILURE; |
131 | goto exit; | |
132 | } | |
5bb633f1 | 133 | |
912541b0 KS |
134 | if (!set_loading(udev, loadpath, "1")) |
135 | goto exit; | |
136 | ||
d5a89d7d | 137 | strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL); |
912541b0 | 138 | if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) { |
baa30fbc | 139 | log_error("error sending firmware '%s' to device\n", firmware); |
912541b0 KS |
140 | set_loading(udev, loadpath, "-1"); |
141 | rc = EXIT_FAILURE; | |
142 | goto exit; | |
143 | }; | |
144 | ||
145 | set_loading(udev, loadpath, "0"); | |
85eaf38c | 146 | exit: |
912541b0 KS |
147 | if (fwfile) |
148 | fclose(fwfile); | |
149 | return rc; | |
85eaf38c KS |
150 | } |
151 | ||
152 | const struct udev_builtin udev_builtin_firmware = { | |
912541b0 KS |
153 | .name = "firmware", |
154 | .cmd = builtin_firmware, | |
155 | .help = "kernel firmware loader", | |
156 | .run_once = true, | |
85eaf38c | 157 | }; |