Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Andreas Schneider <asn@cryptomilk.org> |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 3 | * Copyright (c) 2017 Christopher N. Hesse <raymanfx@gmail.com> |
Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "wifiloader" |
| 19 | #define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <errno.h> |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 22 | #include <fcntl.h> |
Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <cutils/log.h> |
Christopher N. Hesse | 5b6ec56 | 2018-01-22 00:20:06 +0100 | [diff] [blame] | 27 | #include <cutils/properties.h> |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/syscall.h> |
Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 30 | |
Christopher N. Hesse | 5650825 | 2018-01-24 02:14:45 +0100 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
| 32 | |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 33 | #define DEFERRED_INITCALLS "/proc/deferred_initcalls" |
| 34 | |
| 35 | #ifndef WIFI_DRIVER_MODULE_NAME |
| 36 | #define WIFI_DRIVER_MODULE_NAME "wlan" |
| 37 | #endif |
| 38 | |
| 39 | #ifndef WIFI_DRIVER_MODULE_PATH |
| 40 | #define WIFI_DRIVER_MODULE_PATH "/system/lib/modules/" WIFI_DRIVER_MODULE_NAME ".ko" |
| 41 | #endif |
| 42 | |
Christopher N. Hesse | 5650825 | 2018-01-24 02:14:45 +0100 | [diff] [blame] | 43 | #ifndef WIFI_DRIVER_FW_PATH_PARAM |
| 44 | #define WIFI_DRIVER_FW_PATH_PARAM "/sys/module/wlan/parameters/fwpath" |
| 45 | #endif |
| 46 | |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 47 | #define finit_module(fd, params, flags) syscall(__NR_finit_module, fd, params, flags) |
| 48 | |
| 49 | |
| 50 | static int check_module_loaded(char const *tag) |
| 51 | { |
| 52 | FILE *proc; |
| 53 | char line[126]; |
| 54 | |
| 55 | if ((proc = fopen("/proc/modules", "r")) == NULL) { |
| 56 | ALOGW("Could not open %s: %s", "/proc/modules", strerror(errno)); |
| 57 | return -errno; |
| 58 | } |
| 59 | |
| 60 | while ((fgets(line, sizeof(line), proc)) != NULL) { |
| 61 | if (strncmp(line, tag, strlen(tag)) == 0) { |
| 62 | fclose(proc); |
| 63 | return 1; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fclose(proc); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int load_module(char const *path) |
| 72 | { |
| 73 | int fd; |
| 74 | |
| 75 | if (check_module_loaded(WIFI_DRIVER_MODULE_NAME) > 0) { |
| 76 | ALOGE("Driver: %s already loaded", path); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | fd = open(path, O_RDONLY); |
| 81 | if (fd == -1) { |
| 82 | ALOGE("Failed to open %s - error: %s", path, strerror(errno)); |
| 83 | return -errno; |
| 84 | } |
| 85 | |
| 86 | // load the .ko image |
| 87 | if (finit_module(fd, "", 0) != 0) { |
| 88 | ALOGE("Failed to load module %s - error: %s", path, strerror(errno)); |
| 89 | close(fd); |
| 90 | return -errno; |
| 91 | } |
| 92 | |
Christopher N. Hesse | 5650825 | 2018-01-24 02:14:45 +0100 | [diff] [blame] | 93 | // setup proper permissions for firmware change |
| 94 | if (chown(WIFI_DRIVER_FW_PATH_PARAM, AID_WIFI, AID_WIFI) != 0) { |
| 95 | ALOGE("Failed to chown firmware path %s - error: %s", |
| 96 | WIFI_DRIVER_FW_PATH_PARAM, strerror(errno)); |
| 97 | close(fd); |
| 98 | return -errno; |
| 99 | } |
| 100 | |
Christopher N. Hesse | 5b6ec56 | 2018-01-22 00:20:06 +0100 | [diff] [blame] | 101 | // let wifi HAL know we succeeded |
| 102 | ALOGV("Successfully loaded WLAN module: %s", WIFI_DRIVER_MODULE_NAME); |
| 103 | property_set("wlan.driver.status", "ok"); |
| 104 | |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 105 | close(fd); |
| 106 | return 0; |
| 107 | } |
Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 108 | |
| 109 | int main(void) |
| 110 | { |
| 111 | char buf[8] = { '\0' }; |
| 112 | FILE *fp; |
| 113 | size_t r; |
Christopher N. Hesse | 231dc65 | 2017-08-02 15:21:35 +0200 | [diff] [blame] | 114 | struct stat st; |
| 115 | |
| 116 | if (stat(WIFI_DRIVER_MODULE_PATH, &st) == 0) { |
| 117 | ALOGD("Loading WiFi kernel module: %s", WIFI_DRIVER_MODULE_PATH); |
| 118 | load_module(WIFI_DRIVER_MODULE_PATH); |
| 119 | } |
Andreas Schneider | 2140f1f | 2015-12-19 14:29:21 +0100 | [diff] [blame] | 120 | |
| 121 | ALOGD("Trigger initcall of deferred modules\n"); |
| 122 | |
| 123 | fp = fopen(DEFERRED_INITCALLS, "r"); |
| 124 | if (fp == NULL) { |
| 125 | ALOGE("Failed to open %s - error: %s\n", |
| 126 | DEFERRED_INITCALLS, |
| 127 | strerror(errno)); |
| 128 | return -errno; |
| 129 | } |
| 130 | |
| 131 | r = fread(buf, sizeof(buf), 1, fp); |
| 132 | fclose(fp); |
| 133 | |
| 134 | ALOGV("%s=%s\n", DEFERRED_INITCALLS, buf); |
| 135 | |
| 136 | return 0; |
| 137 | } |