blob: c3829120fc4cf2240ededf1fb858a65e12fbac4a [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
39/*
40 * murata:
41 * 00:37:6d
42 * 88:30:8a
43 *
44 * semcosh:
45 * 5c:0a:5b
46 *
47 */
48
49int main() {
50 FILE* file;
51 FILE* cidfile;
52 char* str;
53 char mac_addr_half[9];
54 int ret = -1;
55 int amode;
56 enum Type type = NONE;
57
58 /* open mac addr file */
59 file = fopen(MACADDR_PATH, "r");
60 if(file == 0) {
61 fprintf(stderr, "open(%s) failed\n", MACADDR_PATH);
62 ALOGE("Can't open %s\n", MACADDR_PATH);
63 return -1;
64 }
65
66 /* get and compare mac addr */
67 str = fgets(mac_addr_half, 9, file);
68 if(str == 0) {
69 fprintf(stderr, "fgets() from file %s failed\n", MACADDR_PATH);
70 ALOGE("Can't read from %s\n", MACADDR_PATH);
71 return -1;
72 }
73
74 /* murata */
75 if(strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 ||
76 strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 ||
77 strncasecmp(mac_addr_half, "20:02:af", 9) == 0) {
78 type = MURATA;
79 }
80
81 /* semcosh */
82 if(strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0) {
83 type = SEMCOSH;
84 }
85
86 if (type != NONE) {
87 /* open cid file */
88 cidfile = fopen(CID_PATH, "w");
89 if(cidfile == 0) {
90 fprintf(stderr, "open(%s) failed\n", CID_PATH);
91 ALOGE("Can't open %s\n", CID_PATH);
92 return -1;
93 }
94
95 switch(type) {
96 case NONE:
97 return -1;
98 break;
99 case MURATA:
100 /* write murata to cid file */
101 ALOGI("Writing murata to %s\n", CID_PATH);
102 ret = fputs("murata", cidfile);
103 break;
104 case SEMCOSH:
105 /* write semcosh to cid file */
106 ALOGI("Writing semcosh to %s\n", CID_PATH);
107 ret = fputs("semcosh", cidfile);
108 break;
109 case SEMCOVE:
110 /* write semcove to cid file */
111 ALOGI("Writing semcove to %s\n", CID_PATH);
112 ret = fputs("semcove", cidfile);
113 break;
114 }
115
116 if(ret != 0) {
117 fprintf(stderr, "fputs() to file %s failed\n", CID_PATH);
118 ALOGE("Can't write to %s\n", CID_PATH);
119 return -1;
120 }
121 fclose(cidfile);
122
123 /* set permissions on cid file */
124 ALOGD("Setting permissions on %s\n", CID_PATH);
125 amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
126 ret = chmod(CID_PATH, amode);
127
128 char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH));
129 char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH));
130 sprintf(chown_cmd, "chown system %s", CID_PATH);
131 sprintf(chgrp_cmd, "chgrp system %s", CID_PATH);
132 system(chown_cmd);
133 system(chgrp_cmd);
134
135 if(ret != 0) {
136 fprintf(stderr, "chmod() on file %s failed\n", CID_PATH);
137 ALOGE("Can't set permissions on %s\n", CID_PATH);
138 return ret;
139 }
140
141 } else {
142 /* delete cid file if no specific type */
143 ALOGD("Deleting file %s\n", CID_PATH);
144 remove(CID_PATH);
145 }
146 fclose(file);
147 return 0;
148}