blob: 89b657da54fbed08f35f438dab1749034d4f5a67 [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
Andreas Schneider2b601792015-01-26 23:14:17 +010019#define LOG_TAG "macloader"
20#define LOG_NDEBUG 0
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070021
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
Andreas Schneider7f517d72015-01-26 23:19:03 +010026#include <unistd.h>
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070027
Andreas Schneider2b601792015-01-26 23:14:17 +010028#include <cutils/log.h>
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070029
30#define MACADDR_PATH "/efs/wifi/.mac.info"
31#define CID_PATH "/data/.cid.info"
32
33enum Type {
34 NONE,
35 MURATA,
36 SEMCOSH,
Andreas Schneiderf3ba7202015-01-03 19:29:28 +010037 SEMCOVE,
38 SEMCO3RD,
Christopher N. Hesse0ed56702015-01-25 17:38:23 +010039 SEMCO,
Andreas Schneiderf3ba7202015-01-03 19:29:28 +010040 WISOL
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070041};
42
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070043int main() {
44 FILE* file;
45 FILE* cidfile;
46 char* str;
47 char mac_addr_half[9];
48 int ret = -1;
49 int amode;
50 enum Type type = NONE;
51
52 /* open mac addr file */
53 file = fopen(MACADDR_PATH, "r");
codeworkx84f8d1d2012-12-13 17:23:45 +010054 if (file == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070055 fprintf(stderr, "open(%s) failed\n", MACADDR_PATH);
56 ALOGE("Can't open %s\n", MACADDR_PATH);
57 return -1;
58 }
59
60 /* get and compare mac addr */
61 str = fgets(mac_addr_half, 9, file);
codeworkx84f8d1d2012-12-13 17:23:45 +010062 if (str == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070063 fprintf(stderr, "fgets() from file %s failed\n", MACADDR_PATH);
64 ALOGE("Can't read from %s\n", MACADDR_PATH);
65 return -1;
66 }
67
Liam Alford2fccbdf2013-11-21 13:55:42 +000068 /* murata
69 ref: http://hwaddress.com/?q=ACT */
70 if (strncasecmp(mac_addr_half, "00:0e:6d", 9) == 0 ||
71 strncasecmp(mac_addr_half, "00:13:e0", 9) == 0 ||
72 strncasecmp(mac_addr_half, "00:21:e8", 9) == 0 ||
73 strncasecmp(mac_addr_half, "00:26:e8", 9) == 0 ||
74 strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 ||
75 strncasecmp(mac_addr_half, "00:60:57", 9) == 0 ||
76 strncasecmp(mac_addr_half, "04:46:65", 9) == 0 ||
77 strncasecmp(mac_addr_half, "10:5f:06", 9) == 0 ||
JustArchi300ecd32014-09-28 23:55:36 +020078 strncasecmp(mac_addr_half, "10:a5:d0", 9) == 0 ||
golcheck5b418172013-12-30 13:22:55 +000079 strncasecmp(mac_addr_half, "1c:99:4c", 9) == 0 ||
Liam Alford2fccbdf2013-11-21 13:55:42 +000080 strncasecmp(mac_addr_half, "14:7d:c5", 9) == 0 ||
81 strncasecmp(mac_addr_half, "20:02:af", 9) == 0 ||
82 strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 ||
83 strncasecmp(mac_addr_half, "44:a7:cf", 9) == 0 ||
84 strncasecmp(mac_addr_half, "5c:da:d4", 9) == 0 ||
85 strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 ||
JustArchi300ecd32014-09-28 23:55:36 +020086 strncasecmp(mac_addr_half, "78:4b:87", 9) == 0 ||
Liam Alford2fccbdf2013-11-21 13:55:42 +000087 strncasecmp(mac_addr_half, "60:21:c0", 9) == 0 ||
88 strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 ||
MarcKed973a7b2014-12-30 17:52:55 +010089 strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 ||
90 strncasecmp(mac_addr_half, "fc:c2:de", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070091 type = MURATA;
92 }
93
94 /* semcosh */
MarcKea81d58b2014-12-10 19:12:54 +010095 if (strncasecmp(mac_addr_half, "34:23:ba", 9) == 0 ||
96 strncasecmp(mac_addr_half, "38:aa:3c", 9) == 0 ||
97 strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 ||
98 strncasecmp(mac_addr_half, "88:32:9b", 9) == 0 ||
99 strncasecmp(mac_addr_half, "90:18:7c", 9) == 0 ||
Ethan Chen727dea72013-07-31 13:18:51 -0700100 strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700101 type = SEMCOSH;
102 }
103
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100104 /* semco3rd */
105 if (strncasecmp(mac_addr_half, "f4:09:d8", 9) == 0) {
106 type = SEMCO3RD;
107 }
108
Christopher N. Hesse0ed56702015-01-25 17:38:23 +0100109 /* semco */
110 if (strncasecmp(mac_addr_half, "c0:bd:d1", 9) == 0 ||
111 strncasecmp(mac_addr_half, "51:f6:6b", 9) == 0) {
112 type = SEMCO;
113 }
114
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100115 /* wisol */
116 if (strncasecmp(mac_addr_half, "48:5A:3F", 9) == 0) {
117 type = WISOL;
118 }
119
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700120 if (type != NONE) {
121 /* open cid file */
122 cidfile = fopen(CID_PATH, "w");
123 if(cidfile == 0) {
124 fprintf(stderr, "open(%s) failed\n", CID_PATH);
125 ALOGE("Can't open %s\n", CID_PATH);
126 return -1;
127 }
128
129 switch(type) {
130 case NONE:
131 return -1;
132 break;
133 case MURATA:
134 /* write murata to cid file */
135 ALOGI("Writing murata to %s\n", CID_PATH);
136 ret = fputs("murata", cidfile);
137 break;
138 case SEMCOSH:
139 /* write semcosh to cid file */
140 ALOGI("Writing semcosh to %s\n", CID_PATH);
141 ret = fputs("semcosh", cidfile);
142 break;
143 case SEMCOVE:
144 /* write semcove to cid file */
145 ALOGI("Writing semcove to %s\n", CID_PATH);
146 ret = fputs("semcove", cidfile);
147 break;
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100148 case SEMCO3RD:
149 ALOGI("Writing semco3rd to %s\n", CID_PATH);
150 ret = fputs("semco3rd", cidfile);
151 break;
Christopher N. Hesse0ed56702015-01-25 17:38:23 +0100152 case SEMCO:
153 /* write semco to cid file */
154 ALOGI("Writing semco to %s\n", CID_PATH);
155 ret = fputs("semco", cidfile);
156 break;
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100157 case WISOL:
158 ALOGI("Writing wisol to %s\n", CID_PATH);
159 ret = fputs("wisol", cidfile);
160 break;
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700161 }
162
codeworkx84f8d1d2012-12-13 17:23:45 +0100163 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700164 fprintf(stderr, "fputs() to file %s failed\n", CID_PATH);
165 ALOGE("Can't write to %s\n", CID_PATH);
166 return -1;
167 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700168
Andreas Schneider7f517d72015-01-26 23:19:03 +0100169 /* Change permissions of cid file */
170 ALOGD("Change permissions of %s\n", CID_PATH);
171
172 fd = fileno(cidfile);
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700173 amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
Andreas Schneider7f517d72015-01-26 23:19:03 +0100174 ret = fchmod(fd, amode);
175 fclose(cidfile);
176 if (ret != 0) {
177 ALOGE("Can't set permissions on %s - %s\n",
178 CID_PATH, strerror(errno));
179 return 1;
180 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700181
Javier Ferrer76ea68e2013-05-21 01:09:24 +0200182 char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH) + 1);
183 char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH) + 1);
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700184 sprintf(chown_cmd, "chown system %s", CID_PATH);
185 sprintf(chgrp_cmd, "chgrp system %s", CID_PATH);
186 system(chown_cmd);
187 system(chgrp_cmd);
188
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700189 } else {
190 /* delete cid file if no specific type */
191 ALOGD("Deleting file %s\n", CID_PATH);
192 remove(CID_PATH);
193 }
194 fclose(file);
195 return 0;
196}