blob: 16b0e1be9a428ea77dad2bac523fd0a2bb7dd974 [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,
36 SEMCOVE
37};
38
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070039int 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");
codeworkx84f8d1d2012-12-13 17:23:45 +010050 if (file == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070051 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);
codeworkx84f8d1d2012-12-13 17:23:45 +010058 if (str == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070059 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
64 /* murata */
codeworkx84f8d1d2012-12-13 17:23:45 +010065 if (strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 ||
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070066 strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 ||
codeworkx84f8d1d2012-12-13 17:23:45 +010067 strncasecmp(mac_addr_half, "20:02:af", 9) == 0 ||
Daniel Hillenbrand2b8a73f2013-05-05 12:41:55 +020068 strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 ||
XpLoDWilD731cd202013-08-18 20:55:38 +020069 strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 ||
Dan Pasanen708e7c72013-10-17 22:21:49 -050070 strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 ||
Daniel Hillenbrand2b8a73f2013-05-05 12:41:55 +020071 strncasecmp(mac_addr_half, "60:21:c0", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070072 type = MURATA;
73 }
74
75 /* semcosh */
Ethan Chen727dea72013-07-31 13:18:51 -070076 if (strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 ||
77 strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070078 type = SEMCOSH;
79 }
80
81 if (type != NONE) {
82 /* open cid file */
83 cidfile = fopen(CID_PATH, "w");
84 if(cidfile == 0) {
85 fprintf(stderr, "open(%s) failed\n", CID_PATH);
86 ALOGE("Can't open %s\n", CID_PATH);
87 return -1;
88 }
89
90 switch(type) {
91 case NONE:
92 return -1;
93 break;
94 case MURATA:
95 /* write murata to cid file */
96 ALOGI("Writing murata to %s\n", CID_PATH);
97 ret = fputs("murata", cidfile);
98 break;
99 case SEMCOSH:
100 /* write semcosh to cid file */
101 ALOGI("Writing semcosh to %s\n", CID_PATH);
102 ret = fputs("semcosh", cidfile);
103 break;
104 case SEMCOVE:
105 /* write semcove to cid file */
106 ALOGI("Writing semcove to %s\n", CID_PATH);
107 ret = fputs("semcove", cidfile);
108 break;
109 }
110
codeworkx84f8d1d2012-12-13 17:23:45 +0100111 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700112 fprintf(stderr, "fputs() to file %s failed\n", CID_PATH);
113 ALOGE("Can't write to %s\n", CID_PATH);
114 return -1;
115 }
116 fclose(cidfile);
117
118 /* set permissions on cid file */
119 ALOGD("Setting permissions on %s\n", CID_PATH);
120 amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
121 ret = chmod(CID_PATH, amode);
122
Javier Ferrer76ea68e2013-05-21 01:09:24 +0200123 char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH) + 1);
124 char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH) + 1);
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700125 sprintf(chown_cmd, "chown system %s", CID_PATH);
126 sprintf(chgrp_cmd, "chgrp system %s", CID_PATH);
127 system(chown_cmd);
128 system(chgrp_cmd);
129
codeworkx84f8d1d2012-12-13 17:23:45 +0100130 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700131 fprintf(stderr, "chmod() on file %s failed\n", CID_PATH);
132 ALOGE("Can't set permissions on %s\n", CID_PATH);
133 return ret;
134 }
135
136 } else {
137 /* delete cid file if no specific type */
138 ALOGD("Deleting file %s\n", CID_PATH);
139 remove(CID_PATH);
140 }
141 fclose(file);
142 return 0;
143}