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