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 || |
Dan Pasanen | 708e7c7 | 2013-10-17 22:21:49 -0500 | [diff] [blame^] | 70 | strncasecmp(mac_addr_half, "f0:27:65", 9) == 0 || |
Daniel Hillenbrand | 2b8a73f | 2013-05-05 12:41:55 +0200 | [diff] [blame] | 71 | strncasecmp(mac_addr_half, "60:21:c0", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 72 | type = MURATA; |
| 73 | } |
| 74 | |
| 75 | /* semcosh */ |
Ethan Chen | 727dea7 | 2013-07-31 13:18:51 -0700 | [diff] [blame] | 76 | if (strncasecmp(mac_addr_half, "5c:0a:5b", 9) == 0 || |
| 77 | strncasecmp(mac_addr_half, "cc:3a:61", 9) == 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 78 | 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 | |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 111 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 112 | 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 Ferrer | 76ea68e | 2013-05-21 01:09:24 +0200 | [diff] [blame] | 123 | 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 Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 125 | 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 | |
codeworkx | 84f8d1d | 2012-12-13 17:23:45 +0100 | [diff] [blame] | 130 | if (ret != 0) { |
R. Andrew Ohana | 81c2e05 | 2012-10-03 19:52:52 -0700 | [diff] [blame] | 131 | 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 | } |