Christopher N. Hesse | 222cff0 | 2016-01-01 16:36:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * Copyright (C) 2015 Christopher N. Hesse <raymanfx@gmail.com> |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Andreas Schneider | 87a5957 | 2016-01-04 13:59:17 +0100 | [diff] [blame] | 18 | #define LOG_TAG "modemloader" |
| 19 | #define LOG_NDEBUG 0 |
| 20 | |
Christopher N. Hesse | 222cff0 | 2016-01-01 16:36:37 +0100 | [diff] [blame] | 21 | #include <stdarg.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <ctype.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/un.h> |
| 33 | |
| 34 | #include <cutils/properties.h> |
| 35 | #include <cutils/log.h> |
| 36 | |
Christopher N. Hesse | 222cff0 | 2016-01-01 16:36:37 +0100 | [diff] [blame] | 37 | void parse_hardware_revision(unsigned int *revision) |
| 38 | { |
| 39 | const char *cpuinfo = "/proc/cpuinfo"; |
| 40 | char *data = NULL; |
| 41 | size_t len = 0, limit = 1024; |
| 42 | int fd, n; |
| 43 | char *x, *rev; |
| 44 | |
| 45 | fd = open(cpuinfo, O_RDONLY); |
| 46 | if (fd < 0) return; |
| 47 | |
| 48 | for (;;) { |
| 49 | x = (char*)realloc(data, limit); |
| 50 | if (!x) { |
| 51 | ALOGE("Failed to allocate memory to read %s\n", cpuinfo); |
| 52 | goto done; |
| 53 | } |
| 54 | data = x; |
| 55 | |
| 56 | n = read(fd, data + len, limit - len); |
| 57 | if (n < 0) { |
| 58 | ALOGE("Failed reading %s: %s (%d)\n", cpuinfo, strerror(errno), errno); |
| 59 | goto done; |
| 60 | } |
| 61 | len += n; |
| 62 | |
| 63 | if (len < limit) |
| 64 | break; |
| 65 | |
| 66 | /* We filled the buffer, so increase size and loop to read more */ |
| 67 | limit *= 2; |
| 68 | } |
| 69 | |
| 70 | data[len] = 0; |
| 71 | rev = strstr(data, "\nRevision"); |
| 72 | |
| 73 | if (rev) { |
| 74 | x = strstr(rev, ": "); |
| 75 | if (x) { |
| 76 | *revision = strtoul(x + 2, 0, 16); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | done: |
| 81 | close(fd); |
| 82 | free(data); |
| 83 | } |
| 84 | |
| 85 | int main(void) |
| 86 | { |
| 87 | unsigned int revision = 0; |
| 88 | char ro_revision[PROP_VALUE_MAX]; |
| 89 | |
| 90 | parse_hardware_revision(&revision); |
| 91 | snprintf(ro_revision, PROP_VALUE_MAX, "%d", revision); |
Christopher N. Hesse | 90ced08 | 2016-03-15 19:43:04 +0100 | [diff] [blame] | 92 | property_set("hw.revision", ro_revision); |
Christopher N. Hesse | 222cff0 | 2016-01-01 16:36:37 +0100 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |