R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012, The CyanogenMod Project |
| 3 | * Daniel Hillenbrand <codeworkx@cyanogenmod.com> |
| 4 | * Marco Hillenbrand <marco.hillenbrand@googlemail.com> |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
Andreas Schneider | 2b60179 | 2015-01-26 23:14:17 +0100 | [diff] [blame] | 19 | #define LOG_TAG "macloader" |
| 20 | #define LOG_NDEBUG 0 |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Andreas Schneider | 3ff947b | 2015-01-26 22:56:30 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Andreas Schneider | 3ff947b | 2015-01-26 22:56:30 +0100 | [diff] [blame] | 27 | #include <fcntl.h> |
Andreas Schneider | 7f517d7 | 2015-01-26 23:19:03 +0100 | [diff] [blame] | 28 | #include <unistd.h> |
Andreas Schneider | 7558b9d | 2015-01-26 23:29:18 +0100 | [diff] [blame] | 29 | #include <pwd.h> |
Caio Schnepper | 02c9c71 | 2015-10-10 02:14:28 -0300 | [diff] [blame] | 30 | #include <errno.h> |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 31 | |
Andreas Schneider | 2b60179 | 2015-01-26 23:14:17 +0100 | [diff] [blame] | 32 | #include <cutils/log.h> |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 33 | |
Andreas Schneider | 3ff947b | 2015-01-26 22:56:30 +0100 | [diff] [blame] | 34 | #ifndef WIFI_DRIVER_NVRAM_PATH |
| 35 | #define WIFI_DRIVER_NVRAM_PATH NULL |
| 36 | #endif |
| 37 | |
| 38 | #ifndef WIFI_DRIVER_NVRAM_PATH_PARAM |
| 39 | #define WIFI_DRIVER_NVRAM_PATH_PARAM "/sys/module/wlan/parameters/nvram_path" |
| 40 | #endif |
| 41 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 42 | #define MACADDR_PATH "/efs/wifi/.mac.info" |
| 43 | #define CID_PATH "/data/.cid.info" |
| 44 | |
| 45 | enum Type { |
| 46 | NONE, |
| 47 | MURATA, |
| 48 | SEMCOSH, |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 49 | SEMCOVE, |
| 50 | SEMCO3RD, |
Christopher N. Hesse | 0ed5670 | 2015-01-25 17:38:23 +0100 | [diff] [blame] | 51 | SEMCO, |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 52 | WISOL |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Andreas Schneider | 3ff947b | 2015-01-26 22:56:30 +0100 | [diff] [blame] | 55 | static int wifi_change_nvram_calibration(const char *nvram_file, |
| 56 | const char *type) |
| 57 | { |
| 58 | int len; |
| 59 | int fd = -1; |
| 60 | int ret = 0; |
| 61 | struct stat sb; |
| 62 | char nvram_str[1024] = { 0 }; |
| 63 | |
| 64 | if (nvram_file == NULL || type == NULL) { |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | ret = stat(nvram_file, &sb); |
| 69 | if (ret != 0) { |
| 70 | ALOGE("Failed to check for NVRAM calibration file '%s' - error: %s", |
| 71 | nvram_file, |
| 72 | strerror(errno)); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | ALOGD("Using NVRAM calibration file: %s\n", nvram_file); |
| 77 | |
| 78 | fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_NVRAM_PATH_PARAM, O_WRONLY)); |
| 79 | if (fd < 0) { |
| 80 | ALOGE("Failed to open wifi nvram config path %s - error: %s", |
| 81 | WIFI_DRIVER_NVRAM_PATH_PARAM, strerror(errno)); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | len = strlen(nvram_file) + 1; |
| 86 | if (TEMP_FAILURE_RETRY(write(fd, nvram_file, len)) != len) { |
| 87 | ALOGE("Failed to write to wifi config path %s - error: %s", |
| 88 | WIFI_DRIVER_NVRAM_PATH_PARAM, strerror(errno)); |
| 89 | ret = -1; |
| 90 | goto out; |
| 91 | } |
| 92 | |
| 93 | snprintf(nvram_str, sizeof(nvram_str), "%s_%s", |
| 94 | nvram_file, type); |
| 95 | |
| 96 | ALOGD("Changing NVRAM calibration file for %s chipset\n", type); |
| 97 | |
| 98 | ret = stat(nvram_str, &sb); |
| 99 | if (ret != 0) { |
| 100 | ALOGW("NVRAM calibration file '%s' doesn't exist", nvram_str); |
| 101 | /* |
| 102 | * We were able to write the default calibration file. So |
| 103 | * continue here without returning an error. |
| 104 | */ |
| 105 | ret = 0; |
| 106 | goto out; |
| 107 | } |
| 108 | |
| 109 | len = strlen(nvram_str) + 1; |
| 110 | if (TEMP_FAILURE_RETRY(write(fd, nvram_str, len)) != len) { |
| 111 | ALOGW("Failed to write to wifi config path %s - error: %s", |
| 112 | WIFI_DRIVER_NVRAM_PATH_PARAM, strerror(errno)); |
| 113 | /* |
| 114 | * We were able to write the default calibration file. So |
| 115 | * continue here without returning an error. |
| 116 | */ |
| 117 | ret = 0; |
| 118 | goto out; |
| 119 | } |
| 120 | |
| 121 | ALOGD("NVRAM calibration file set to '%s'\n", nvram_str); |
| 122 | |
| 123 | ret = 0; |
| 124 | out: |
| 125 | if (fd != -1) { |
| 126 | close(fd); |
| 127 | } |
| 128 | return ret; |
| 129 | } |
| 130 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 131 | int main() { |
| 132 | FILE* file; |
| 133 | FILE* cidfile; |
| 134 | char* str; |
| 135 | char mac_addr_half[9]; |
| 136 | int ret = -1; |
| 137 | int amode; |
| 138 | enum Type type = NONE; |
| 139 | |
| 140 | /* open mac addr file */ |
| 141 | file = fopen(MACADDR_PATH, "r"); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 142 | if (file == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 143 | fprintf(stderr, "open(%s) failed\n", MACADDR_PATH); |
| 144 | ALOGE("Can't open %s\n", MACADDR_PATH); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /* get and compare mac addr */ |
| 149 | str = fgets(mac_addr_half, 9, file); |
Andreas Schneider | 3a73bd3 | 2015-01-26 23:30:13 +0100 | [diff] [blame] | 150 | fclose(file); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 151 | if (str == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 152 | fprintf(stderr, "fgets() from file %s failed\n", MACADDR_PATH); |
| 153 | ALOGE("Can't read from %s\n", MACADDR_PATH); |
| 154 | return -1; |
| 155 | } |
| 156 | |
Liam Alford | 2fccbdf | 2013-11-21 13:55:42 +0000 | [diff] [blame] | 157 | /* murata |
| 158 | ref: http://hwaddress.com/?q=ACT */ |
| 159 | if (strncasecmp(mac_addr_half, "00:0e:6d", 9) == 0 || |
| 160 | strncasecmp(mac_addr_half, "00:13:e0", 9) == 0 || |
| 161 | strncasecmp(mac_addr_half, "00:21:e8", 9) == 0 || |
| 162 | strncasecmp(mac_addr_half, "00:26:e8", 9) == 0 || |
| 163 | strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 || |
| 164 | strncasecmp(mac_addr_half, "00:60:57", 9) == 0 || |
| 165 | strncasecmp(mac_addr_half, "04:46:65", 9) == 0 || |
| 166 | strncasecmp(mac_addr_half, "10:5f:06", 9) == 0 || |
JustArchi | 300ecd3 | 2014-09-28 23:55:36 +0200 | [diff] [blame] | 167 | strncasecmp(mac_addr_half, "10:a5:d0", 9) == 0 || |
golcheck | 5b41817 | 2013-12-30 13:22:55 +0000 | [diff] [blame] | 168 | strncasecmp(mac_addr_half, "1c:99:4c", 9) == 0 || |
Liam Alford | 2fccbdf | 2013-11-21 13:55:42 +0000 | [diff] [blame] | 169 | strncasecmp(mac_addr_half, "14:7d:c5", 9) == 0 || |
| 170 | strncasecmp(mac_addr_half, "20:02:af", 9) == 0 || |
| 171 | strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 || |
| 172 | strncasecmp(mac_addr_half, "44:a7:cf", 9) == 0 || |
| 173 | strncasecmp(mac_addr_half, "5c:da:d4", 9) == 0 || |
| 174 | strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 || |
JustArchi | 300ecd3 | 2014-09-28 23:55:36 +0200 | [diff] [blame] | 175 | strncasecmp(mac_addr_half, "78:4b:87", 9) == 0 || |
RGIB | 02302a7 | 2016-03-11 15:56:23 +0100 | [diff] [blame] | 176 | strncasecmp(mac_addr_half, "78:52:1A", 9) == 0 || |
Liam Alford | 2fccbdf | 2013-11-21 13:55:42 +0000 | [diff] [blame] | 177 | strncasecmp(mac_addr_half, "60:21:c0", 9) == 0 || |
| 178 | strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 || |
MarcKe | d973a7b | 2014-12-30 17:52:55 +0100 | [diff] [blame] | 179 | strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 || |
| 180 | strncasecmp(mac_addr_half, "fc:c2:de", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 181 | type = MURATA; |
| 182 | } |
| 183 | |
| 184 | /* semcosh */ |
MarcKe | a81d58b | 2014-12-10 19:12:54 +0100 | [diff] [blame] | 185 | if (strncasecmp(mac_addr_half, "34:23:ba", 9) == 0 || |
| 186 | strncasecmp(mac_addr_half, "38:aa:3c", 9) == 0 || |
| 187 | strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 || |
| 188 | strncasecmp(mac_addr_half, "88:32:9b", 9) == 0 || |
| 189 | strncasecmp(mac_addr_half, "90:18:7c", 9) == 0 || |
Ethan Chen | 727dea7 | 2013-07-31 13:18:51 -0700 | [diff] [blame] | 190 | strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 191 | type = SEMCOSH; |
| 192 | } |
| 193 | |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 194 | /* semco3rd */ |
| 195 | if (strncasecmp(mac_addr_half, "f4:09:d8", 9) == 0) { |
| 196 | type = SEMCO3RD; |
| 197 | } |
| 198 | |
Christopher N. Hesse | 0ed5670 | 2015-01-25 17:38:23 +0100 | [diff] [blame] | 199 | /* semco */ |
Christopher N. Hesse | 438a27e | 2016-02-29 17:55:11 +0100 | [diff] [blame] | 200 | if (strncasecmp(mac_addr_half, "51:f6:6b", 9) == 0 || |
| 201 | strncasecmp(mac_addr_half, "c0:bd:d1", 9) == 0 || |
| 202 | strncasecmp(mac_addr_half, "ec:9b:f3", 9) == 0) { |
Christopher N. Hesse | 0ed5670 | 2015-01-25 17:38:23 +0100 | [diff] [blame] | 203 | type = SEMCO; |
| 204 | } |
| 205 | |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 206 | /* wisol */ |
| 207 | if (strncasecmp(mac_addr_half, "48:5A:3F", 9) == 0) { |
| 208 | type = WISOL; |
| 209 | } |
| 210 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 211 | if (type != NONE) { |
Andreas Schneider | 3ff947b | 2015-01-26 22:56:30 +0100 | [diff] [blame] | 212 | const char *nvram_file; |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 213 | const char *type_str; |
Andreas Schneider | 7558b9d | 2015-01-26 23:29:18 +0100 | [diff] [blame] | 214 | struct passwd *pwd; |
| 215 | int fd; |
| 216 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 217 | /* open cid file */ |
| 218 | cidfile = fopen(CID_PATH, "w"); |
| 219 | if(cidfile == 0) { |
| 220 | fprintf(stderr, "open(%s) failed\n", CID_PATH); |
| 221 | ALOGE("Can't open %s\n", CID_PATH); |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | switch(type) { |
| 226 | case NONE: |
| 227 | return -1; |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 228 | case MURATA: |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 229 | type_str = "murata"; |
| 230 | break; |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 231 | case SEMCOSH: |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 232 | type_str = "semcosh"; |
| 233 | break; |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 234 | case SEMCOVE: |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 235 | type_str = "semcove"; |
| 236 | break; |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 237 | case SEMCO3RD: |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 238 | type_str = "semco3rd"; |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 239 | break; |
Christopher N. Hesse | 0ed5670 | 2015-01-25 17:38:23 +0100 | [diff] [blame] | 240 | case SEMCO: |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 241 | type_str = "semco"; |
| 242 | break; |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 243 | case WISOL: |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 244 | type_str = "wisol"; |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 245 | break; |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 246 | } |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 247 | |
Andreas Schneider | c7212d0 | 2015-01-26 22:45:26 +0100 | [diff] [blame] | 248 | ALOGI("Settting wifi type to %s in %s\n", type_str, CID_PATH); |
| 249 | |
| 250 | ret = fputs(type_str, cidfile); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 251 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 252 | ALOGE("Can't write to %s\n", CID_PATH); |
Andreas Schneider | ca1a0ee | 2015-01-26 22:49:49 +0100 | [diff] [blame] | 253 | return 1; |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 254 | } |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 255 | |
Andreas Schneider | 7f517d7 | 2015-01-26 23:19:03 +0100 | [diff] [blame] | 256 | /* Change permissions of cid file */ |
| 257 | ALOGD("Change permissions of %s\n", CID_PATH); |
| 258 | |
| 259 | fd = fileno(cidfile); |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 260 | amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
Andreas Schneider | 7f517d7 | 2015-01-26 23:19:03 +0100 | [diff] [blame] | 261 | ret = fchmod(fd, amode); |
Andreas Schneider | 7f517d7 | 2015-01-26 23:19:03 +0100 | [diff] [blame] | 262 | if (ret != 0) { |
Andreas Schneider | 7558b9d | 2015-01-26 23:29:18 +0100 | [diff] [blame] | 263 | fclose(cidfile); |
Andreas Schneider | 7f517d7 | 2015-01-26 23:19:03 +0100 | [diff] [blame] | 264 | ALOGE("Can't set permissions on %s - %s\n", |
| 265 | CID_PATH, strerror(errno)); |
| 266 | return 1; |
| 267 | } |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 268 | |
Andreas Schneider | 7558b9d | 2015-01-26 23:29:18 +0100 | [diff] [blame] | 269 | pwd = getpwnam("system"); |
| 270 | if (pwd == NULL) { |
| 271 | fclose(cidfile); |
| 272 | ALOGE("Failed to find 'system' user - %s\n", |
| 273 | strerror(errno)); |
| 274 | return 1; |
| 275 | } |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 276 | |
Andreas Schneider | 7558b9d | 2015-01-26 23:29:18 +0100 | [diff] [blame] | 277 | ret = fchown(fd, pwd->pw_uid, pwd->pw_gid); |
| 278 | fclose(cidfile); |
| 279 | if (ret != 0) { |
| 280 | ALOGE("Failed to change owner of %s - %s\n", |
| 281 | CID_PATH, strerror(errno)); |
| 282 | return 1; |
| 283 | } |
Andreas Schneider | 3ff947b | 2015-01-26 22:56:30 +0100 | [diff] [blame] | 284 | |
| 285 | nvram_file = WIFI_DRIVER_NVRAM_PATH; |
| 286 | if (nvram_file != NULL) { |
| 287 | ret = wifi_change_nvram_calibration(nvram_file, type_str); |
| 288 | if (ret != 0) { |
| 289 | return 1; |
| 290 | } |
| 291 | } |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 292 | } else { |
| 293 | /* delete cid file if no specific type */ |
| 294 | ALOGD("Deleting file %s\n", CID_PATH); |
| 295 | remove(CID_PATH); |
| 296 | } |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | } |