R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | enum Type { |
| 33 | NONE, |
| 34 | MURATA, |
| 35 | SEMCOSH, |
| 36 | SEMCOVE |
| 37 | }; |
| 38 | |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 39 | int 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"); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 50 | if (file == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 51 | 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); |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 58 | if (str == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 59 | 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 */ |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 65 | if (strncasecmp(mac_addr_half, "00:37:6d", 9) == 0 || |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 66 | strncasecmp(mac_addr_half, "88:30:8a", 9) == 0 || |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 67 | strncasecmp(mac_addr_half, "20:02:af", 9) == 0 || |
Daniel Hillenbrand | 2b8a73f | 2013-05-05 12:41:55 +0200 | [diff] [blame] | 68 | strncasecmp(mac_addr_half, "5c:f8:a1", 9) == 0 || |
XpLoDWilD | 731cd20 | 2013-08-18 20:55:38 +0200 | [diff] [blame^] | 69 | strncasecmp(mac_addr_half, "40:f3:08", 9) == 0 || |
Daniel Hillenbrand | 2b8a73f | 2013-05-05 12:41:55 +0200 | [diff] [blame] | 70 | strncasecmp(mac_addr_half, "60:21:c0", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 71 | type = MURATA; |
| 72 | } |
| 73 | |
| 74 | /* semcosh */ |
Ethan Chen | 727dea7 | 2013-07-31 13:18:51 -0700 | [diff] [blame] | 75 | if (strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 || |
| 76 | strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 77 | type = SEMCOSH; |
| 78 | } |
| 79 | |
| 80 | if (type != NONE) { |
| 81 | /* open cid file */ |
| 82 | cidfile = fopen(CID_PATH, "w"); |
| 83 | if(cidfile == 0) { |
| 84 | fprintf(stderr, "open(%s) failed\n", CID_PATH); |
| 85 | ALOGE("Can't open %s\n", CID_PATH); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | switch(type) { |
| 90 | case NONE: |
| 91 | return -1; |
| 92 | break; |
| 93 | case MURATA: |
| 94 | /* write murata to cid file */ |
| 95 | ALOGI("Writing murata to %s\n", CID_PATH); |
| 96 | ret = fputs("murata", cidfile); |
| 97 | break; |
| 98 | case SEMCOSH: |
| 99 | /* write semcosh to cid file */ |
| 100 | ALOGI("Writing semcosh to %s\n", CID_PATH); |
| 101 | ret = fputs("semcosh", cidfile); |
| 102 | break; |
| 103 | case SEMCOVE: |
| 104 | /* write semcove to cid file */ |
| 105 | ALOGI("Writing semcove to %s\n", CID_PATH); |
| 106 | ret = fputs("semcove", cidfile); |
| 107 | break; |
| 108 | } |
| 109 | |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 110 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 111 | fprintf(stderr, "fputs() to file %s failed\n", CID_PATH); |
| 112 | ALOGE("Can't write to %s\n", CID_PATH); |
| 113 | return -1; |
| 114 | } |
| 115 | fclose(cidfile); |
| 116 | |
| 117 | /* set permissions on cid file */ |
| 118 | ALOGD("Setting permissions on %s\n", CID_PATH); |
| 119 | amode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
| 120 | ret = chmod(CID_PATH, amode); |
| 121 | |
Javier Ferrer | 76ea68e | 2013-05-21 01:09:24 +0200 | [diff] [blame] | 122 | char* chown_cmd = (char*) malloc(strlen("chown system ") + strlen(CID_PATH) + 1); |
| 123 | char* chgrp_cmd = (char*) malloc(strlen("chgrp system ") + strlen(CID_PATH) + 1); |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 124 | sprintf(chown_cmd, "chown system %s", CID_PATH); |
| 125 | sprintf(chgrp_cmd, "chgrp system %s", CID_PATH); |
| 126 | system(chown_cmd); |
| 127 | system(chgrp_cmd); |
| 128 | |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 129 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 130 | fprintf(stderr, "chmod() on file %s failed\n", CID_PATH); |
| 131 | ALOGE("Can't set permissions on %s\n", CID_PATH); |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | } else { |
| 136 | /* delete cid file if no specific type */ |
| 137 | ALOGD("Deleting file %s\n", CID_PATH); |
| 138 | remove(CID_PATH); |
| 139 | } |
| 140 | fclose(file); |
| 141 | return 0; |
| 142 | } |