blob: fc65bf3dd1234ffa98e75e3e229d1c18c0c2b21d [file] [log] [blame]
R. Andrew Ohana81c2e052012-10-03 19:52:52 -07001/*
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
32enum Type {
33 NONE,
34 MURATA,
35 SEMCOSH,
Andreas Schneiderf3ba7202015-01-03 19:29:28 +010036 SEMCOVE,
37 SEMCO3RD,
Christopher N. Hesse0ed56702015-01-25 17:38:23 +010038 SEMCO,
Andreas Schneiderf3ba7202015-01-03 19:29:28 +010039 WISOL
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070040};
41
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070042int main() {
43 FILE* file;
44 FILE* cidfile;
45 char* str;
46 char mac_addr_half[9];
47 int ret = -1;
48 int amode;
49 enum Type type = NONE;
50
51 /* open mac addr file */
52 file = fopen(MACADDR_PATH, "r");
codeworkx84f8d1d2012-12-13 17:23:45 +010053 if (file == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070054 fprintf(stderr, "open(%s) failed\n", MACADDR_PATH);
55 ALOGE("Can't open %s\n", MACADDR_PATH);
56 return -1;
57 }
58
59 /* get and compare mac addr */
60 str = fgets(mac_addr_half, 9, file);
codeworkx84f8d1d2012-12-13 17:23:45 +010061 if (str == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070062 fprintf(stderr, "fgets() from file %s failed\n", MACADDR_PATH);
63 ALOGE("Can't read from %s\n", MACADDR_PATH);
64 return -1;
65 }
66
Liam Alford2fccbdf2013-11-21 13:55:42 +000067 /* murata
68 ref: http://hwaddress.com/?q=ACT */
69 if (strncasecmp(mac_addr_half, "00:0e:6d", 9) == 0 ||
70 strncasecmp(mac_addr_half, "00:13:e0", 9) == 0 ||
71 strncasecmp(mac_addr_half, "00:21:e8", 9) == 0 ||
72 strncasecmp(mac_addr_half, "00:26:e8", 9) == 0 ||
73 strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 ||
74 strncasecmp(mac_addr_half, "00:60:57", 9) == 0 ||
75 strncasecmp(mac_addr_half, "04:46:65", 9) == 0 ||
76 strncasecmp(mac_addr_half, "10:5f:06", 9) == 0 ||
JustArchi300ecd32014-09-28 23:55:36 +020077 strncasecmp(mac_addr_half, "10:a5:d0", 9) == 0 ||
golcheck5b418172013-12-30 13:22:55 +000078 strncasecmp(mac_addr_half, "1c:99:4c", 9) == 0 ||
Liam Alford2fccbdf2013-11-21 13:55:42 +000079 strncasecmp(mac_addr_half, "14:7d:c5", 9) == 0 ||
80 strncasecmp(mac_addr_half, "20:02:af", 9) == 0 ||
81 strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 ||
82 strncasecmp(mac_addr_half, "44:a7:cf", 9) == 0 ||
83 strncasecmp(mac_addr_half, "5c:da:d4", 9) == 0 ||
84 strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 ||
JustArchi300ecd32014-09-28 23:55:36 +020085 strncasecmp(mac_addr_half, "78:4b:87", 9) == 0 ||
Liam Alford2fccbdf2013-11-21 13:55:42 +000086 strncasecmp(mac_addr_half, "60:21:c0", 9) == 0 ||
87 strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 ||
MarcKed973a7b2014-12-30 17:52:55 +010088 strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 ||
89 strncasecmp(mac_addr_half, "fc:c2:de", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070090 type = MURATA;
91 }
92
93 /* semcosh */
MarcKea81d58b2014-12-10 19:12:54 +010094 if (strncasecmp(mac_addr_half, "34:23:ba", 9) == 0 ||
95 strncasecmp(mac_addr_half, "38:aa:3c", 9) == 0 ||
96 strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 ||
97 strncasecmp(mac_addr_half, "88:32:9b", 9) == 0 ||
98 strncasecmp(mac_addr_half, "90:18:7c", 9) == 0 ||
Ethan Chen727dea72013-07-31 13:18:51 -070099 strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700100 type = SEMCOSH;
101 }
102
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100103 /* semco3rd */
104 if (strncasecmp(mac_addr_half, "f4:09:d8", 9) == 0) {
105 type = SEMCO3RD;
106 }
107
Christopher N. Hesse0ed56702015-01-25 17:38:23 +0100108 /* semco */
109 if (strncasecmp(mac_addr_half, "c0:bd:d1", 9) == 0 ||
110 strncasecmp(mac_addr_half, "51:f6:6b", 9) == 0) {
111 type = SEMCO;
112 }
113
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100114 /* wisol */
115 if (strncasecmp(mac_addr_half, "48:5A:3F", 9) == 0) {
116 type = WISOL;
117 }
118
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700119 if (type != NONE) {
120 /* open cid file */
121 cidfile = fopen(CID_PATH, "w");
122 if(cidfile == 0) {
123 fprintf(stderr, "open(%s) failed\n", CID_PATH);
124 ALOGE("Can't open %s\n", CID_PATH);
125 return -1;
126 }
127
128 switch(type) {
129 case NONE:
130 return -1;
131 break;
132 case MURATA:
133 /* write murata to cid file */
134 ALOGI("Writing murata to %s\n", CID_PATH);
135 ret = fputs("murata", cidfile);
136 break;
137 case SEMCOSH:
138 /* write semcosh to cid file */
139 ALOGI("Writing semcosh to %s\n", CID_PATH);
140 ret = fputs("semcosh", cidfile);
141 break;
142 case SEMCOVE:
143 /* write semcove to cid file */
144 ALOGI("Writing semcove to %s\n", CID_PATH);
145 ret = fputs("semcove", cidfile);
146 break;
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100147 case SEMCO3RD:
148 ALOGI("Writing semco3rd to %s\n", CID_PATH);
149 ret = fputs("semco3rd", cidfile);
150 break;
Christopher N. Hesse0ed56702015-01-25 17:38:23 +0100151 case SEMCO:
152 /* write semco to cid file */
153 ALOGI("Writing semco to %s\n", CID_PATH);
154 ret = fputs("semco", cidfile);
155 break;
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100156 case WISOL:
157 ALOGI("Writing wisol to %s\n", CID_PATH);
158 ret = fputs("wisol", cidfile);
159 break;
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700160 }
161
codeworkx84f8d1d2012-12-13 17:23:45 +0100162 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700163 fprintf(stderr, "fputs() to file %s failed\n", CID_PATH);
164 ALOGE("Can't write to %s\n", CID_PATH);
165 return -1;
166 }
167 fclose(cidfile);
168
169 /* set permissions on cid file */
170 ALOGD("Setting permissions on %s\n", CID_PATH);
171 amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
172 ret = chmod(CID_PATH, amode);
173
Javier Ferrer76ea68e2013-05-21 01:09:24 +0200174 char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH) + 1);
175 char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH) + 1);
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700176 sprintf(chown_cmd, "chown system %s", CID_PATH);
177 sprintf(chgrp_cmd, "chgrp system %s", CID_PATH);
178 system(chown_cmd);
179 system(chgrp_cmd);
180
codeworkx84f8d1d2012-12-13 17:23:45 +0100181 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700182 fprintf(stderr, "chmod() on file %s failed\n", CID_PATH);
183 ALOGE("Can't set permissions on %s\n", CID_PATH);
184 return ret;
185 }
186
187 } else {
188 /* delete cid file if no specific type */
189 ALOGD("Deleting file %s\n", CID_PATH);
190 remove(CID_PATH);
191 }
192 fclose(file);
193 return 0;
194}