]>
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 | ||
9ec6e95b | 30 | static bool set_loading(struct udev *udev, char *loadpath, const char *state) { |
912541b0 KS |
31 | FILE *ldfile; |
32 | ||
33 | ldfile = fopen(loadpath, "we"); | |
34 | if (ldfile == NULL) { | |
9f6445e3 | 35 | log_error("error: can not open '%s'", loadpath); |
912541b0 KS |
36 | return false; |
37 | }; | |
38 | fprintf(ldfile, "%s\n", state); | |
39 | fclose(ldfile); | |
40 | return true; | |
85eaf38c KS |
41 | } |
42 | ||
9ec6e95b | 43 | static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size) { |
912541b0 KS |
44 | char *buf; |
45 | FILE *fsource = NULL, *ftarget = NULL; | |
46 | bool ret = false; | |
47 | ||
48 | buf = malloc(size); | |
49 | if (buf == NULL) { | |
baa30fbc | 50 | log_error("No memory available to load firmware file"); |
912541b0 KS |
51 | return false; |
52 | } | |
53 | ||
9f6445e3 | 54 | log_debug("writing '%s' (%zi) to '%s'", source, size, target); |
912541b0 KS |
55 | |
56 | fsource = fopen(source, "re"); | |
57 | if (fsource == NULL) | |
58 | goto exit; | |
59 | ftarget = fopen(target, "we"); | |
60 | if (ftarget == NULL) | |
61 | goto exit; | |
62 | if (fread(buf, size, 1, fsource) != 1) | |
63 | goto exit; | |
64 | if (fwrite(buf, size, 1, ftarget) == 1) | |
65 | ret = true; | |
85eaf38c | 66 | exit: |
912541b0 KS |
67 | if (ftarget != NULL) |
68 | fclose(ftarget); | |
69 | if (fsource != NULL) | |
70 | fclose(fsource); | |
71 | free(buf); | |
72 | return ret; | |
85eaf38c KS |
73 | } |
74 | ||
9ec6e95b | 75 | static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test) { |
912541b0 KS |
76 | struct udev *udev = udev_device_get_udev(dev); |
77 | static const char *searchpath[] = { FIRMWARE_PATH }; | |
912541b0 KS |
78 | char loadpath[UTIL_PATH_SIZE]; |
79 | char datapath[UTIL_PATH_SIZE]; | |
80 | char fwpath[UTIL_PATH_SIZE]; | |
81 | const char *firmware; | |
b49d9b50 | 82 | FILE *fwfile = NULL; |
912541b0 KS |
83 | struct utsname kernel; |
84 | struct stat statbuf; | |
85 | unsigned int i; | |
86 | int rc = EXIT_SUCCESS; | |
87 | ||
88 | firmware = udev_device_get_property_value(dev, "FIRMWARE"); | |
89 | if (firmware == NULL) { | |
9f6445e3 | 90 | log_error("firmware parameter missing"); |
912541b0 KS |
91 | rc = EXIT_FAILURE; |
92 | goto exit; | |
93 | } | |
94 | ||
95 | /* lookup firmware file */ | |
96 | uname(&kernel); | |
8fef0ff2 | 97 | for (i = 0; i < ELEMENTSOF(searchpath); i++) { |
d5a89d7d | 98 | strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL); |
912541b0 KS |
99 | fwfile = fopen(fwpath, "re"); |
100 | if (fwfile != NULL) | |
101 | break; | |
102 | ||
d5a89d7d | 103 | strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL); |
912541b0 KS |
104 | fwfile = fopen(fwpath, "re"); |
105 | if (fwfile != NULL) | |
106 | break; | |
107 | } | |
108 | ||
d5a89d7d | 109 | strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL); |
912541b0 KS |
110 | |
111 | if (fwfile == NULL) { | |
9f6445e3 | 112 | log_debug("did not find firmware file '%s'", firmware); |
912541b0 | 113 | rc = EXIT_FAILURE; |
39177382 KS |
114 | /* |
115 | * Do not cancel the request in the initrd, the real root might have | |
116 | * the firmware file and the 'coldplug' run in the real root will find | |
117 | * this pending request and fulfill or cancel it. | |
118 | * */ | |
119 | if (!in_initrd()) | |
120 | set_loading(udev, loadpath, "-1"); | |
912541b0 KS |
121 | goto exit; |
122 | } | |
123 | ||
124 | if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) { | |
5bb633f1 UTL |
125 | if (!in_initrd()) |
126 | set_loading(udev, loadpath, "-1"); | |
912541b0 KS |
127 | rc = EXIT_FAILURE; |
128 | goto exit; | |
129 | } | |
5bb633f1 | 130 | |
912541b0 KS |
131 | if (!set_loading(udev, loadpath, "1")) |
132 | goto exit; | |
133 | ||
d5a89d7d | 134 | strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL); |
912541b0 | 135 | if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) { |
9f6445e3 | 136 | log_error("error sending firmware '%s' to device", firmware); |
912541b0 KS |
137 | set_loading(udev, loadpath, "-1"); |
138 | rc = EXIT_FAILURE; | |
139 | goto exit; | |
140 | }; | |
141 | ||
142 | set_loading(udev, loadpath, "0"); | |
85eaf38c | 143 | exit: |
912541b0 KS |
144 | if (fwfile) |
145 | fclose(fwfile); | |
146 | return rc; | |
85eaf38c KS |
147 | } |
148 | ||
149 | const struct udev_builtin udev_builtin_firmware = { | |
912541b0 KS |
150 | .name = "firmware", |
151 | .cmd = builtin_firmware, | |
152 | .help = "kernel firmware loader", | |
153 | .run_once = true, | |
85eaf38c | 154 | }; |