blob: 62397cd6f902d18af5187191a37460bc3043a684 [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 ||
69 strncasecmp(mac_addr_half, "60:21:c0", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070070 type = MURATA;
71 }
72
73 /* semcosh */
codeworkx84f8d1d2012-12-13 17:23:45 +010074 if (strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -070075 type = SEMCOSH;
76 }
77
78 if (type != NONE) {
79 /* open cid file */
80 cidfile = fopen(CID_PATH, "w");
81 if(cidfile == 0) {
82 fprintf(stderr, "open(%s) failed\n", CID_PATH);
83 ALOGE("Can't open %s\n", CID_PATH);
84 return -1;
85 }
86
87 switch(type) {
88 case NONE:
89 return -1;
90 break;
91 case MURATA:
92 /* write murata to cid file */
93 ALOGI("Writing murata to %s\n", CID_PATH);
94 ret = fputs("murata", cidfile);
95 break;
96 case SEMCOSH:
97 /* write semcosh to cid file */
98 ALOGI("Writing semcosh to %s\n", CID_PATH);
99 ret = fputs("semcosh", cidfile);
100 break;
101 case SEMCOVE:
102 /* write semcove to cid file */
103 ALOGI("Writing semcove to %s\n", CID_PATH);
104 ret = fputs("semcove", cidfile);
105 break;
106 }
107
codeworkx84f8d1d2012-12-13 17:23:45 +0100108 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700109 fprintf(stderr, "fputs() to file %s failed\n", CID_PATH);
110 ALOGE("Can't write to %s\n", CID_PATH);
111 return -1;
112 }
113 fclose(cidfile);
114
115 /* set permissions on cid file */
116 ALOGD("Setting permissions on %s\n", CID_PATH);
117 amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
118 ret = chmod(CID_PATH, amode);
119
120 char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH));
121 char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH));
122 sprintf(chown_cmd, "chown system %s", CID_PATH);
123 sprintf(chgrp_cmd, "chgrp system %s", CID_PATH);
124 system(chown_cmd);
125 system(chgrp_cmd);
126
codeworkx84f8d1d2012-12-13 17:23:45 +0100127 if (ret != 0) {
R. Andrew Ohana81c2e052012-10-03 19:52:52 -0700128 fprintf(stderr, "chmod() on file %s failed\n", CID_PATH);
129 ALOGE("Can't set permissions on %s\n", CID_PATH);
130 return ret;
131 }
132
133 } else {
134 /* delete cid file if no specific type */
135 ALOGD("Deleting file %s\n", CID_PATH);
136 remove(CID_PATH);
137 }
138 fclose(file);
139 return 0;
140}