blob: f19e1aff201a0fd7a3005f25efa3cba7d261f7a3 [file] [log] [blame]
Christopher N. Hesse222cff02016-01-01 16:36:37 +01001/*
2 * Copyright (C) 2008 The Android Open Source Project
Christopher N. Hesseb0d86102017-05-25 17:39:18 +02003 * Copyright (C) 2015-2017 Christopher N. Hesse <raymanfx@gmail.com>
Christopher N. Hesse222cff02016-01-01 16:36:37 +01004 *
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 Schneider87a59572016-01-04 13:59:17 +010018#define LOG_TAG "modemloader"
19#define LOG_NDEBUG 0
20
Christopher N. Hesse222cff02016-01-01 16:36:37 +010021#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. Hesse222cff02016-01-01 16:36:37 +010037void 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
80done:
81 close(fd);
82 free(data);
83}
84
85int main(void)
86{
Christopher N. Hesseb0d86102017-05-25 17:39:18 +020087 unsigned int revision;
88 char hardware_revision[PROP_VALUE_MAX];
89 const char *properties[] = {"hw.revision", "ro.cbd.dt_revision", "ril.cbd.dt_revision"};
Christopher N. Hesse222cff02016-01-01 16:36:37 +010090
Christopher N. Hesseb0d86102017-05-25 17:39:18 +020091 // try to read the revision from the kernel cmdline
92 revision = property_get_int32("ro.boot.hw_rev", 0);
93
94 // if the property was not exported, try to parse /proc/cpuinfo
95 if (revision == 0) {
96 parse_hardware_revision(&revision);
97 }
98
99 snprintf(hardware_revision, PROP_VALUE_MAX, "%d", revision);
100
101 // set all the properties
102 const int array_length = sizeof(properties) / sizeof(properties[0]);
103 for (int i = 0; i < array_length; i++) {
104 property_set(properties[i], hardware_revision);
105 }
106
107 // indicate that we are done
108 property_set("ro.modemloader.done", "1");
Christopher N. Hesse222cff02016-01-01 16:36:37 +0100109
110 return 0;
111}