aboutsummaryrefslogtreecommitdiff
path: root/init/init_mofd.cpp
blob: 2505b3ed0e7d6fc3153b4ed208a0682961df7841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
** Copyright 2016, The CyanogenMod Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>

#include "log.h"
#include "property_service.h"
#include "util.h"
#include "vendor_init.h"

#define STRCONV_(x)      #x
#define STRCONV(x) "%" STRCONV_(x) "s"

#define PHONE_INFO "/factory/PhoneInfodisk/PhoneInfo_inf"
#define BUF_SIZE 64

/* Serial number */
#define SERIAL_PROP "ro.serialno"
#define SERIAL_OFFSET 0x00
#define SERIAL_LENGTH 17

/* Cpufreq */
#define MAX_CPU_FREQ    "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
#define LOW_CPU "1833000"
#define HIGH_CPU "2333000"

/* Zram */
#define ZRAM_PROP "ro.config.zram"
#define MEMINFO_FILE "/proc/meminfo"
#define MEMINFO_KEY "MemTotal:"
#define ZRAM_MEM_THRESHOLD 3000000

/* Intel kernel paths */
char const *intel_path[] = {
"/sys/kernel/fw_update/fw_info/ifwi_version",
"/sys/kernel/fw_update/fw_info/chaabi_version",
"/sys/kernel/fw_update/fw_info/mia_version",
"/sys/kernel/fw_update/fw_info/scu_bs_version",
"/sys/kernel/fw_update/fw_info/scu_version",
"/sys/kernel/fw_update/fw_info/ia32fw_version",
"/sys/kernel/fw_update/fw_info/valhooks_version",
"/sys/kernel/fw_update/fw_info/punit_version",
"/sys/kernel/fw_update/fw_info/ucode_version",
"/sys/kernel/fw_update/fw_info/pmic_nvm_version",
"/sys/devices/virtual/misc/watchdog/counter",
"/proc/sys/kernel/osrelease"
};

/* Intel props */
char const *intel_prop[] = {
"sys.ifwi.version",
"sys.chaabi.version",
"sys.mia.version",
"sys.scu_bs.version",
"sys.scu.version",
"sys.ia32fw.version",
"sys.valhooks.version",
"sys.punit.version",
"sys.ucode.version",
"sys.pmic_nvm.version",
"sys.watchdog.previous.counter",
"sys.kernel.version"
};

static int read_file2(const char *fname, char *data, int max_size)
{
    int fd, rc;

    if (max_size < 1)
        return 0;

    fd = open(fname, O_RDONLY);
    if (fd < 0) {
        ERROR("failed to open '%s'\n", fname);
        return 0;
    }

    rc = read(fd, data, max_size -1);
    if ((rc > 0) && (rc < max_size ))
        data[rc] = '\0';
    else
        data[0] = '\0';
    close(fd);

    return 1;
}

static void get_serial()
{
    int ret = 0;
    char const *path = PHONE_INFO;
    char buf[SERIAL_LENGTH + 1];
    char value[BUF_SIZE];
    prop_info *pi;

    if(read_file2(path, buf, sizeof(buf))) {
        if (strlen(buf) > 0) {
            sscanf(buf, STRCONV(BUF_SIZE), value);
            property_set(SERIAL_PROP,value);
        }
    }
}

static void configure_zram() {
    char buf[128];
    FILE *f;

    if ((f = fopen(MEMINFO_FILE, "r")) == NULL) {
        ERROR("%s: Failed to open %s\n", __func__, MEMINFO_FILE);
        return;
    }

    while (fgets(buf, sizeof(buf), f) != NULL) {
        if (strncmp(buf, MEMINFO_KEY, strlen(MEMINFO_KEY)) == 0) {
            int mem = atoi(&buf[strlen(MEMINFO_KEY)]);
            const char *mode = mem < ZRAM_MEM_THRESHOLD ? "true" : "false";
            INFO("%s: Found total memory to be %d kb, zram enabled: %s\n", __func__, mem, mode);
            property_set(ZRAM_PROP, mode);
            break;
        }
    }

    fclose(f);
}

static void intel_props() {

    char buf[BUF_SIZE];
    char value[BUF_SIZE];
    for(int i=0; i<12; i++) {
        if(read_file2(intel_path[i], buf, sizeof(buf))) {
            sscanf(buf, STRCONV(BUF_SIZE), value);
            property_set(intel_prop[i],value);
        }
    }

}

void set_feq_values()
{
    char buf[BUF_SIZE];

    if(read_file2(MAX_CPU_FREQ, buf, sizeof(buf))) {
	if ( strncmp(buf, LOW_CPU, strlen(LOW_CPU)) == 0 ) {
            property_set("ro.sys.perf.device.powersave", "1250000");
            property_set("ro.sys.perf.device.touchboost", "500000");
            property_set("ro.sys.perf.device.full", "1833000");
        } else if ( strncmp(buf, HIGH_CPU, strlen(HIGH_CPU)) == 0 ) {
            property_set("ro.sys.perf.device.powersave", "1500000");
            property_set("ro.sys.perf.device.touchboost", "1833000");
            property_set("ro.sys.perf.device.full", "2333000");
        } else {
            INFO("%s: Failed to get max cpu speed: %s\n", __func__, buf);
        }
    }
}

void vendor_load_properties()
{
    get_serial();
    configure_zram();
    intel_props();
    set_feq_values();
}