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 | |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <cutils/log.h> |
| 25 | |
| 26 | #define LOG_TAG "macloader" |
| 27 | #define LOG_NDEBUG 0 |
| 28 | |
| 29 | #define MACADDR_PATH "/efs/wifi/.mac.info" |
| 30 | #define CID_PATH "/data/.cid.info" |
| 31 | |
| 32 | enum Type { |
| 33 | NONE, |
| 34 | MURATA, |
| 35 | SEMCOSH, |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 36 | SEMCOVE, |
| 37 | SEMCO3RD, |
| 38 | WISOL |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 41 | int main() { |
| 42 | FILE* file; |
| 43 | FILE* cidfile; |
| 44 | char* str; |
| 45 | char mac_addr_half[9]; |
| 46 | int ret = -1; |
| 47 | int amode; |
| 48 | enum Type type = NONE; |
| 49 | |
| 50 | /* open mac addr file */ |
| 51 | file = fopen(MACADDR_PATH, "r"); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 52 | if (file == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 53 | fprintf(stderr, "open(%s) failed\n", MACADDR_PATH); |
| 54 | ALOGE("Can't open %s\n", MACADDR_PATH); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | /* get and compare mac addr */ |
| 59 | str = fgets(mac_addr_half, 9, file); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 60 | if (str == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 61 | fprintf(stderr, "fgets() from file %s failed\n", MACADDR_PATH); |
| 62 | ALOGE("Can't read from %s\n", MACADDR_PATH); |
| 63 | return -1; |
| 64 | } |
| 65 | |
Liam Alford | 2fccbdf | 2013-11-21 13:55:42 +0000 | [diff] [blame] | 66 | /* murata |
| 67 | ref: http://hwaddress.com/?q=ACT */ |
| 68 | if (strncasecmp(mac_addr_half, "00:0e:6d", 9) == 0 || |
| 69 | strncasecmp(mac_addr_half, "00:13:e0", 9) == 0 || |
| 70 | strncasecmp(mac_addr_half, "00:21:e8", 9) == 0 || |
| 71 | strncasecmp(mac_addr_half, "00:26:e8", 9) == 0 || |
| 72 | strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 || |
| 73 | strncasecmp(mac_addr_half, "00:60:57", 9) == 0 || |
| 74 | strncasecmp(mac_addr_half, "04:46:65", 9) == 0 || |
| 75 | strncasecmp(mac_addr_half, "10:5f:06", 9) == 0 || |
JustArchi | 300ecd3 | 2014-09-28 23:55:36 +0200 | [diff] [blame] | 76 | strncasecmp(mac_addr_half, "10:a5:d0", 9) == 0 || |
golcheck | 5b41817 | 2013-12-30 13:22:55 +0000 | [diff] [blame] | 77 | strncasecmp(mac_addr_half, "1c:99:4c", 9) == 0 || |
Liam Alford | 2fccbdf | 2013-11-21 13:55:42 +0000 | [diff] [blame] | 78 | strncasecmp(mac_addr_half, "14:7d:c5", 9) == 0 || |
| 79 | strncasecmp(mac_addr_half, "20:02:af", 9) == 0 || |
| 80 | strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 || |
| 81 | strncasecmp(mac_addr_half, "44:a7:cf", 9) == 0 || |
| 82 | strncasecmp(mac_addr_half, "5c:da:d4", 9) == 0 || |
| 83 | strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 || |
JustArchi | 300ecd3 | 2014-09-28 23:55:36 +0200 | [diff] [blame] | 84 | strncasecmp(mac_addr_half, "78:4b:87", 9) == 0 || |
Liam Alford | 2fccbdf | 2013-11-21 13:55:42 +0000 | [diff] [blame] | 85 | strncasecmp(mac_addr_half, "60:21:c0", 9) == 0 || |
| 86 | strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 || |
MarcKe | d973a7b | 2014-12-30 17:52:55 +0100 | [diff] [blame] | 87 | strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 || |
| 88 | strncasecmp(mac_addr_half, "fc:c2:de", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 89 | type = MURATA; |
| 90 | } |
| 91 | |
| 92 | /* semcosh */ |
MarcKe | a81d58b | 2014-12-10 19:12:54 +0100 | [diff] [blame] | 93 | if (strncasecmp(mac_addr_half, "34:23:ba", 9) == 0 || |
| 94 | strncasecmp(mac_addr_half, "38:aa:3c", 9) == 0 || |
| 95 | strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 || |
| 96 | strncasecmp(mac_addr_half, "88:32:9b", 9) == 0 || |
| 97 | strncasecmp(mac_addr_half, "90:18:7c", 9) == 0 || |
Ethan Chen | 727dea7 | 2013-07-31 13:18:51 -0700 | [diff] [blame] | 98 | strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 99 | type = SEMCOSH; |
| 100 | } |
| 101 | |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 102 | /* semco3rd */ |
| 103 | if (strncasecmp(mac_addr_half, "f4:09:d8", 9) == 0) { |
| 104 | type = SEMCO3RD; |
| 105 | } |
| 106 | |
| 107 | /* wisol */ |
| 108 | if (strncasecmp(mac_addr_half, "48:5A:3F", 9) == 0) { |
| 109 | type = WISOL; |
| 110 | } |
| 111 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 112 | if (type != NONE) { |
| 113 | /* open cid file */ |
| 114 | cidfile = fopen(CID_PATH, "w"); |
| 115 | if(cidfile == 0) { |
| 116 | fprintf(stderr, "open(%s) failed\n", CID_PATH); |
| 117 | ALOGE("Can't open %s\n", CID_PATH); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | switch(type) { |
| 122 | case NONE: |
| 123 | return -1; |
| 124 | break; |
| 125 | case MURATA: |
| 126 | /* write murata to cid file */ |
| 127 | ALOGI("Writing murata to %s\n", CID_PATH); |
| 128 | ret = fputs("murata", cidfile); |
| 129 | break; |
| 130 | case SEMCOSH: |
| 131 | /* write semcosh to cid file */ |
| 132 | ALOGI("Writing semcosh to %s\n", CID_PATH); |
| 133 | ret = fputs("semcosh", cidfile); |
| 134 | break; |
| 135 | case SEMCOVE: |
| 136 | /* write semcove to cid file */ |
| 137 | ALOGI("Writing semcove to %s\n", CID_PATH); |
| 138 | ret = fputs("semcove", cidfile); |
| 139 | break; |
Andreas Schneider | f3ba720 | 2015-01-03 19:29:28 +0100 | [diff] [blame] | 140 | case SEMCO3RD: |
| 141 | ALOGI("Writing semco3rd to %s\n", CID_PATH); |
| 142 | ret = fputs("semco3rd", cidfile); |
| 143 | break; |
| 144 | case WISOL: |
| 145 | ALOGI("Writing wisol to %s\n", CID_PATH); |
| 146 | ret = fputs("wisol", cidfile); |
| 147 | break; |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 148 | } |
| 149 | |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 150 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 151 | fprintf(stderr, "fputs() to file %s failed\n", CID_PATH); |
| 152 | ALOGE("Can't write to %s\n", CID_PATH); |
| 153 | return -1; |
| 154 | } |
| 155 | fclose(cidfile); |
| 156 | |
| 157 | /* set permissions on cid file */ |
| 158 | ALOGD("Setting permissions on %s\n", CID_PATH); |
| 159 | amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
| 160 | ret = chmod(CID_PATH, amode); |
| 161 | |
Javier Ferrer | 76ea68e | 2013-05-21 01:09:24 +0200 | [diff] [blame] | 162 | char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH) + 1); |
| 163 | char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH) + 1); |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 164 | sprintf(chown_cmd, "chown system %s", CID_PATH); |
| 165 | sprintf(chgrp_cmd, "chgrp system %s", CID_PATH); |
| 166 | system(chown_cmd); |
| 167 | system(chgrp_cmd); |
| 168 | |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 169 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 170 | fprintf(stderr, "chmod() on file %s failed\n", CID_PATH); |
| 171 | ALOGE("Can't set permissions on %s\n", CID_PATH); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | } else { |
| 176 | /* delete cid file if no specific type */ |
| 177 | ALOGD("Deleting file %s\n", CID_PATH); |
| 178 | remove(CID_PATH); |
| 179 | } |
| 180 | fclose(file); |
| 181 | return 0; |
| 182 | } |