blob: 0e29e28038ab942caaf02f57433b29703f4231d2 [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>
Andreas Schneider7558b9d2015-01-26 23:29:18 +010027#include <pwd.h>
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070028
Andreas Schneider2b601792015-01-26 23:14:17 +010029#include <cutils/log.h>
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070030
31#define MACADDR_PATH "/efs/wifi/.mac.info"
32#define CID_PATH "/data/.cid.info"
33
34enum Type {
35 NONE,
36 MURATA,
37 SEMCOSH,
Andreas Schneiderf3ba7202015-01-03 19:29:28 +010038 SEMCOVE,
39 SEMCO3RD,
Christopher N. Hesse0ed56702015-01-25 17:38:23 +010040 SEMCO,
Andreas Schneiderf3ba7202015-01-03 19:29:28 +010041 WISOL
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070042};
43
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070044int main() {
45 FILE* file;
46 FILE* cidfile;
47 char* str;
48 char mac_addr_half[9];
49 int ret = -1;
50 int amode;
51 enum Type type = NONE;
52
53 /* open mac addr file */
54 file = fopen(MACADDR_PATH, "r");
codeworkx84f8d1d2012-12-13 17:23:45 +010055 if (file == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070056 fprintf(stderr, "open(%s) failed\n", MACADDR_PATH);
57 ALOGE("Can't open %s\n", MACADDR_PATH);
58 return -1;
59 }
60
61 /* get and compare mac addr */
62 str = fgets(mac_addr_half, 9, file);
Andreas Schneider3a73bd32015-01-26 23:30:13 +010063 fclose(file);
codeworkx84f8d1d2012-12-13 17:23:45 +010064 if (str == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070065 fprintf(stderr, "fgets() from file %s failed\n", MACADDR_PATH);
66 ALOGE("Can't read from %s\n", MACADDR_PATH);
67 return -1;
68 }
69
Liam Alford2fccbdf2013-11-21 13:55:42 +000070 /* murata
71 ref: http://hwaddress.com/?q=ACT */
72 if (strncasecmp(mac_addr_half, "00:0e:6d", 9) == 0 ||
73 strncasecmp(mac_addr_half, "00:13:e0", 9) == 0 ||
74 strncasecmp(mac_addr_half, "00:21:e8", 9) == 0 ||
75 strncasecmp(mac_addr_half, "00:26:e8", 9) == 0 ||
76 strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 ||
77 strncasecmp(mac_addr_half, "00:60:57", 9) == 0 ||
78 strncasecmp(mac_addr_half, "04:46:65", 9) == 0 ||
79 strncasecmp(mac_addr_half, "10:5f:06", 9) == 0 ||
JustArchi300ecd32014-09-28 23:55:36 +020080 strncasecmp(mac_addr_half, "10:a5:d0", 9) == 0 ||
golcheck5b418172013-12-30 13:22:55 +000081 strncasecmp(mac_addr_half, "1c:99:4c", 9) == 0 ||
Liam Alford2fccbdf2013-11-21 13:55:42 +000082 strncasecmp(mac_addr_half, "14:7d:c5", 9) == 0 ||
83 strncasecmp(mac_addr_half, "20:02:af", 9) == 0 ||
84 strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 ||
85 strncasecmp(mac_addr_half, "44:a7:cf", 9) == 0 ||
86 strncasecmp(mac_addr_half, "5c:da:d4", 9) == 0 ||
87 strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 ||
JustArchi300ecd32014-09-28 23:55:36 +020088 strncasecmp(mac_addr_half, "78:4b:87", 9) == 0 ||
Liam Alford2fccbdf2013-11-21 13:55:42 +000089 strncasecmp(mac_addr_half, "60:21:c0", 9) == 0 ||
90 strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 ||
MarcKed973a7b2014-12-30 17:52:55 +010091 strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 ||
92 strncasecmp(mac_addr_half, "fc:c2:de", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070093 type = MURATA;
94 }
95
96 /* semcosh */
MarcKea81d58b2014-12-10 19:12:54 +010097 if (strncasecmp(mac_addr_half, "34:23:ba", 9) == 0 ||
98 strncasecmp(mac_addr_half, "38:aa:3c", 9) == 0 ||
99 strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 ||
100 strncasecmp(mac_addr_half, "88:32:9b", 9) == 0 ||
101 strncasecmp(mac_addr_half, "90:18:7c", 9) == 0 ||
Ethan Chen727dea72013-07-31 13:18:51 -0700102 strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700103 type = SEMCOSH;
104 }
105
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100106 /* semco3rd */
107 if (strncasecmp(mac_addr_half, "f4:09:d8", 9) == 0) {
108 type = SEMCO3RD;
109 }
110
Christopher N. Hesse0ed56702015-01-25 17:38:23 +0100111 /* semco */
112 if (strncasecmp(mac_addr_half, "c0:bd:d1", 9) == 0 ||
113 strncasecmp(mac_addr_half, "51:f6:6b", 9) == 0) {
114 type = SEMCO;
115 }
116
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100117 /* wisol */
118 if (strncasecmp(mac_addr_half, "48:5A:3F", 9) == 0) {
119 type = WISOL;
120 }
121
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700122 if (type != NONE) {
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100123 const char *type_str;
Andreas Schneider7558b9d2015-01-26 23:29:18 +0100124 struct passwd *pwd;
125 int fd;
126
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700127 /* open cid file */
128 cidfile = fopen(CID_PATH, "w");
129 if(cidfile == 0) {
130 fprintf(stderr, "open(%s) failed\n", CID_PATH);
131 ALOGE("Can't open %s\n", CID_PATH);
132 return -1;
133 }
134
135 switch(type) {
136 case NONE:
137 return -1;
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700138 case MURATA:
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100139 type_str = "murata";
140 break;
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700141 case SEMCOSH:
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100142 type_str = "semcosh";
143 break;
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700144 case SEMCOVE:
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100145 type_str = "semcove";
146 break;
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100147 case SEMCO3RD:
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100148 type_str = "semco3rd";
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100149 break;
Christopher N. Hesse0ed56702015-01-25 17:38:23 +0100150 case SEMCO:
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100151 type_str = "semco";
152 break;
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100153 case WISOL:
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100154 type_str = "wisol";
Andreas Schneiderf3ba7202015-01-03 19:29:28 +0100155 break;
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100156 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700157
Andreas Schneiderc7212d02015-01-26 22:45:26 +0100158 ALOGI("Settting wifi type to %s in %s\n", type_str, CID_PATH);
159
160 ret = fputs(type_str, cidfile);
codeworkx84f8d1d2012-12-13 17:23:45 +0100161 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700162 ALOGE("Can't write to %s\n", CID_PATH);
Andreas Schneiderca1a0ee2015-01-26 22:49:49 +0100163 return 1;
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700164 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700165
Andreas Schneider7f517d72015-01-26 23:19:03 +0100166 /* Change permissions of cid file */
167 ALOGD("Change permissions of %s\n", CID_PATH);
168
169 fd = fileno(cidfile);
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700170 amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
Andreas Schneider7f517d72015-01-26 23:19:03 +0100171 ret = fchmod(fd, amode);
Andreas Schneider7f517d72015-01-26 23:19:03 +0100172 if (ret != 0) {
Andreas Schneider7558b9d2015-01-26 23:29:18 +0100173 fclose(cidfile);
Andreas Schneider7f517d72015-01-26 23:19:03 +0100174 ALOGE("Can't set permissions on %s - %s\n",
175 CID_PATH, strerror(errno));
176 return 1;
177 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700178
Andreas Schneider7558b9d2015-01-26 23:29:18 +0100179 pwd = getpwnam("system");
180 if (pwd == NULL) {
181 fclose(cidfile);
182 ALOGE("Failed to find 'system' user - %s\n",
183 strerror(errno));
184 return 1;
185 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700186
Andreas Schneider7558b9d2015-01-26 23:29:18 +0100187 ret = fchown(fd, pwd->pw_uid, pwd->pw_gid);
188 fclose(cidfile);
189 if (ret != 0) {
190 ALOGE("Failed to change owner of %s - %s\n",
191 CID_PATH, strerror(errno));
192 return 1;
193 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700194 } else {
195 /* delete cid file if no specific type */
196 ALOGD("Deleting file %s\n", CID_PATH);
197 remove(CID_PATH);
198 }
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700199 return 0;
200}