blob: 867c289c956470208add8ef882a6171eb34c677f [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <linux/input.h>
18#include <pthread.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <time.h>
30#include <unistd.h>
31#include <stdlib.h>
32
33#include <string>
34#include <utility>
35#include <map>
36#include <fstream>
37#include <sstream>
38
39#include "variables.h"
40#include "data.hpp"
41#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040042#include "twrp-functions.hpp"
Dees_Troy2f9117a2013-02-17 19:52:09 -060043#include "gui/blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040044
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070045#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
46 #include "cutils/properties.h"
47#endif
48
Dees_Troy51a0e822012-09-05 15:24:24 -040049extern "C"
50{
Dees_Troy2673cec2013-04-02 20:22:16 +000051 #include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040052 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040053 #include "gui/pages.h"
54
55 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040056}
57
58#define FILE_VERSION 0x00010001
59
60using namespace std;
61
62map<string, DataManager::TStrIntPair> DataManager::mValues;
63map<string, string> DataManager::mConstValues;
64string DataManager::mBackingFile;
65int DataManager::mInitialized = 0;
Dees_Troy2f9117a2013-02-17 19:52:09 -060066extern blanktimer blankTimer;
Dees_Troy51a0e822012-09-05 15:24:24 -040067
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040068// Device ID functions
69void DataManager::sanitize_device_id(char* device_id) {
70 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
71 char str[50];
72 char* c = str;
73
74 strcpy(str, device_id);
75 memset(device_id, 0, sizeof(device_id));
76 while (*c) {
77 if (strchr(whitelist, *c))
78 strncat(device_id, c, 1);
79 c++;
80 }
81 return;
82}
83
84#define CMDLINE_SERIALNO "androidboot.serialno="
85#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
86#define CPUINFO_SERIALNO "Serial"
87#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
88#define CPUINFO_HARDWARE "Hardware"
89#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
90
91void DataManager::get_device_id(void) {
92 FILE *fp;
93 char line[2048];
94 char hardware_id[32], device_id[64];
95 char* token;
96
97 // Assign a blank device_id to start with
98 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070099
100#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
101 // Now we'll use product_model_hardwareid as device id
102 char model_id[PROPERTY_VALUE_MAX];
103 property_get("ro.product.model", model_id, "error");
104 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000105 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700106 // Replace spaces with underscores
107 for(int i = 0; i < strlen(model_id); i++) {
108 if(model_id[i] == ' ')
109 model_id[i] = '_';
110 }
111 strcpy(device_id, model_id);
112 if (hardware_id[0] != 0) {
113 strcat(device_id, "_");
114 strcat(device_id, hardware_id);
115 }
116 sanitize_device_id((char *)device_id);
117 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700119 return;
120 }
121#endif
122
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400123#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
124 // First, try the cmdline to see if the serial number was supplied
125 fp = fopen("/proc/cmdline", "rt");
126 if (fp != NULL)
127 {
128 // First step, read the line. For cmdline, it's one long line
129 fgets(line, sizeof(line), fp);
130 fclose(fp);
131
132 // Now, let's tokenize the string
133 token = strtok(line, " ");
134
135 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
136 while (token)
137 {
138 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
139 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
140 {
141 // We found the serial number!
142 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
143 sanitize_device_id((char *)device_id);
144 mConstValues.insert(make_pair("device_id", device_id));
145 return;
146 }
147 token = strtok(NULL, " ");
148 }
149 }
150#endif
151 // Now we'll try cpuinfo for a serial number
152 fp = fopen("/proc/cpuinfo", "rt");
153 if (fp != NULL)
154 {
155 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
156 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
157 {
158 // We found the serial number!
159 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
160 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
161 if (*token != 0) {
162 token[30] = 0;
163 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
164 memset(device_id, 0, sizeof(device_id));
165 strncpy(device_id, token, strlen(token) - 1);
166 } else {
167 strcpy(device_id, token);
168 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000169 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400170 fclose(fp);
171 sanitize_device_id((char *)device_id);
172 mConstValues.insert(make_pair("device_id", device_id));
173 return;
174 }
175 } else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
176 // We found the hardware ID
177 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
178 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
179 if (*token != 0) {
180 token[30] = 0;
181 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
182 memset(hardware_id, 0, sizeof(hardware_id));
183 strncpy(hardware_id, token, strlen(token) - 1);
184 } else {
185 strcpy(hardware_id, token);
186 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000187 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400188 }
189 }
190 }
191 fclose(fp);
192 }
193
194 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400196 strcpy(device_id, hardware_id);
197 sanitize_device_id((char *)device_id);
198 mConstValues.insert(make_pair("device_id", device_id));
199 return;
200 }
201
202 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000203 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400204 mConstValues.insert(make_pair("device_id", device_id));
205 return;
206}
207
Dees_Troy51a0e822012-09-05 15:24:24 -0400208int DataManager::ResetDefaults()
209{
210 mValues.clear();
211 mConstValues.clear();
212 SetDefaultValues();
213 return 0;
214}
215
216int DataManager::LoadValues(const string filename)
217{
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400218 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
220 if (!mInitialized)
221 SetDefaultValues();
222
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400223 GetValue("device_id", dev_id);
224 // Save off the backing file for set operations
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 mBackingFile = filename;
226
227 // Read in the file, if possible
228 FILE* in = fopen(filename.c_str(), "rb");
Dees_Troya13d74f2013-03-24 08:54:55 -0500229 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000230 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500231 return 0;
232 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000233 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500234 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400235
236 int file_version;
237 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
238 if (file_version != FILE_VERSION) goto error;
239
240 while (!feof(in))
241 {
242 string Name;
243 string Value;
244 unsigned short length;
245 char array[512];
246
247 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
248 if (length >= 512) goto error;
249 if (fread(array, 1, length, in) != length) goto error;
250 Name = array;
251
252 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
253 if (length >= 512) goto error;
254 if (fread(array, 1, length, in) != length) goto error;
255 Value = array;
256
257 map<string, TStrIntPair>::iterator pos;
258
259 pos = mValues.find(Name);
260 if (pos != mValues.end())
261 {
262 pos->second.first = Value;
263 pos->second.second = 1;
264 }
265 else
266 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 if (Name == "tw_screen_timeout_secs")
268 blankTimer.setTime(atoi(Value.c_str()));
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400270error:
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 string current = GetCurrentStoragePath();
273 string settings = GetSettingsStoragePath();
274 if (current != settings && !PartitionManager.Mount_By_Path(current, false)) {
275 SetValue("tw_storage_path", settings);
276 } else {
277 SetBackupFolder();
278 }
279 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400280}
281
282int DataManager::Flush()
283{
284 return SaveValues();
285}
286
287int DataManager::SaveValues()
288{
289 if (mBackingFile.empty()) return -1;
290
291 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
294 FILE* out = fopen(mBackingFile.c_str(), "wb");
295 if (!out) return -1;
296
297 int file_version = FILE_VERSION;
298 fwrite(&file_version, 1, sizeof(int), out);
299
300 map<string, TStrIntPair>::iterator iter;
301 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
302 {
303 // Save only the persisted data
304 if (iter->second.second != 0)
305 {
306 unsigned short length = (unsigned short) iter->first.length() + 1;
307 fwrite(&length, 1, sizeof(unsigned short), out);
308 fwrite(iter->first.c_str(), 1, length, out);
309 length = (unsigned short) iter->second.first.length() + 1;
310 fwrite(&length, 1, sizeof(unsigned short), out);
311 fwrite(iter->second.first.c_str(), 1, length, out);
312 }
313 }
314 fclose(out);
315 return 0;
316}
317
318int DataManager::GetValue(const string varName, string& value)
319{
320 string localStr = varName;
321
322 if (!mInitialized)
323 SetDefaultValues();
324
325 // Strip off leading and trailing '%' if provided
326 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
327 {
328 localStr.erase(0, 1);
329 localStr.erase(localStr.length() - 1, 1);
330 }
331
332 // Handle magic values
333 if (GetMagicValue(localStr, value) == 0) return 0;
334
335 map<string, string>::iterator constPos;
336 constPos = mConstValues.find(localStr);
337 if (constPos != mConstValues.end())
338 {
339 value = constPos->second;
340 return 0;
341 }
342
343 map<string, TStrIntPair>::iterator pos;
344 pos = mValues.find(localStr);
345 if (pos == mValues.end())
346 return -1;
347
348 value = pos->second.first;
349 return 0;
350}
351
352int DataManager::GetValue(const string varName, int& value)
353{
354 string data;
355
356 if (GetValue(varName,data) != 0)
357 return -1;
358
359 value = atoi(data.c_str());
360 return 0;
361}
362
Dees_Troy2673cec2013-04-02 20:22:16 +0000363int DataManager::GetValue(const string varName, float& value)
364{
365 string data;
366
367 if (GetValue(varName,data) != 0)
368 return -1;
369
370 value = atof(data.c_str());
371 return 0;
372}
373
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500374unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
375{
376 string data;
377
378 if (GetValue(varName,data) != 0)
379 return -1;
380
381 value = strtoull(data.c_str(), NULL, 10);
382 return 0;
383}
384
Dees_Troy51a0e822012-09-05 15:24:24 -0400385// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
386string& DataManager::GetValueRef(const string varName)
387{
388 if (!mInitialized)
389 SetDefaultValues();
390
391 map<string, string>::iterator constPos;
392 constPos = mConstValues.find(varName);
393 if (constPos != mConstValues.end())
394 return constPos->second;
395
396 map<string, TStrIntPair>::iterator pos;
397 pos = mValues.find(varName);
398 if (pos == mValues.end())
399 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
400
401 return pos->second.first;
402}
403
404// This function will return an empty string if the value doesn't exist
405string DataManager::GetStrValue(const string varName)
406{
407 string retVal;
408
409 GetValue(varName, retVal);
410 return retVal;
411}
412
413// This function will return 0 if the value doesn't exist
414int DataManager::GetIntValue(const string varName)
415{
416 string retVal;
417
418 GetValue(varName, retVal);
419 return atoi(retVal.c_str());
420}
421
422int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
423{
424 if (!mInitialized)
425 SetDefaultValues();
426
427 // Don't allow empty values or numerical starting values
428 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
429 return -1;
430
431 map<string, string>::iterator constChk;
432 constChk = mConstValues.find(varName);
433 if (constChk != mConstValues.end())
434 return -1;
435
436 map<string, TStrIntPair>::iterator pos;
437 pos = mValues.find(varName);
438 if (pos == mValues.end())
439 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
440 else
441 pos->second.first = value;
442
443 if (pos->second.second != 0)
444 SaveValues();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500445 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600446 blankTimer.setTime(atoi(value.c_str()));
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 } else if (varName == "tw_storage_path") {
448 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500449 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 gui_notifyVarChange(varName.c_str(), value.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 return 0;
452}
453
454int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
455{
456 ostringstream valStr;
457 valStr << value;
458 if (varName == "tw_use_external_storage") {
459 string str;
460
461 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
462 if (value == 0) {
463 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 } else {
465 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 }
467 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
468 str = GetStrValue(TW_INTERNAL_PATH);
469 else
470 str = GetStrValue(TW_EXTERNAL_PATH);
471
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475}
476
477int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
478{
479 ostringstream valStr;
480 valStr << value;
481 return SetValue(varName, valStr.str(), persist);;
482}
483
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500484int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
485{
486 ostringstream valStr;
487 valStr << value;
488 return SetValue(varName, valStr.str(), persist);;
489}
490
Dees_Troy2673cec2013-04-02 20:22:16 +0000491int DataManager::SetProgress(float Fraction) {
492 return SetValue("ui_progress", (float) (Fraction * 100.0));
493}
494
495int DataManager::ShowProgress(float Portion, float Seconds) {
496 float Starting_Portion;
497 GetValue("ui_progress_portion", Starting_Portion);
498 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
499 return -1;
500 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
501 return -1;
502 return 0;
503}
504
Dees_Troy51a0e822012-09-05 15:24:24 -0400505void DataManager::DumpValues()
506{
507 map<string, TStrIntPair>::iterator iter;
Dees_Troy2673cec2013-04-02 20:22:16 +0000508 gui_print("Data Manager dump - Values with leading X are persisted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
510 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000511 gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 }
513}
514
Dees_Troy8170a922012-09-18 15:40:25 -0400515void DataManager::update_tz_environment_variables(void) {
516 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
517 tzset();
518}
519
Dees_Troy16b74352012-11-14 22:27:31 +0000520void DataManager::SetBackupFolder()
521{
522 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000524 str += "/TWRP/BACKUPS/";
525
526 string dev_id;
527 GetValue("device_id", dev_id);
528
529 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000531 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (partition != NULL) {
533 SetValue("tw_storage_display_name", partition->Storage_Name);
534 char free_space[255];
535 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
536 SetValue("tw_storage_free_size", free_space);
537 string zip_path, zip_root, storage_path;
538 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
539 if (partition->Has_Data_Media)
540 storage_path = partition->Symlink_Mount_Point;
541 else
542 storage_path = partition->Storage_Path;
543 if (zip_path.size() < storage_path.size()) {
544 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
545 } else {
546 zip_root= zip_path;
547 zip_root.resize(storage_path.size() + 1);
548 if (zip_root != storage_path)
549 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
550 }
551 } else {
552 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000553 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500554 }
Dees_Troy16b74352012-11-14 22:27:31 +0000555}
556
Dees_Troy51a0e822012-09-05 15:24:24 -0400557void DataManager::SetDefaultValues()
558{
559 string str, path;
560
561 get_device_id();
562
563 mInitialized = 1;
564
565 mConstValues.insert(make_pair("true", "1"));
566 mConstValues.insert(make_pair("false", "0"));
567
568 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400570
Dees_Troyf4499812013-01-23 19:07:38 +0000571#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
572 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
573#endif
574
Dees_Troy51a0e822012-09-05 15:24:24 -0400575#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000576 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
578#else
579 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
580#endif
581
582#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000583 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400584 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
585#else
586 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
587#endif
588
589#ifdef TW_INTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 LOGINFO("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400591 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
592 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000593 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
595 path.clear();
596 path = "/";
597 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
598 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
599 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000600 LOGINFO("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 // Device has dual storage
602 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
603 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
604 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
605 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
606 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
607 path.clear();
608 path = "/";
609 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
610 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
611 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
612 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
613 } else {
614 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
615 }
616 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000617 LOGINFO("Just has internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400618 // Just has internal storage
619 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
620 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
621 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
622 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
623 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
624 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
625 #endif
626#else
627 #ifdef RECOVERY_SDCARD_ON_DATA
628 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000629 LOGINFO("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400630 // Device has /data/media + external storage
631 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
632 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000633 LOGINFO("Single storage only -- data/media.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400634 // Device just has external storage
635 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
636 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
637 #endif
638 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000639 LOGINFO("Single storage only.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400640 // Device just has external storage
641 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
642 #endif
643 #ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000644 LOGINFO("Device has /data/media defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400645 // Device has /data/media
646 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
647 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000648 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400649 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
650 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
651 #ifdef TW_EXTERNAL_STORAGE_PATH
652 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
653 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
654 } else {
655 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
656 }
657 #else
658 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
659 #endif
660 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000661 LOGINFO("No internal storage defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 // Device has no internal storage
663 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
664 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000665 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400666 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
667 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
668 #endif
669 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 LOGINFO("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400671 // External has custom definition
672 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
673 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
674 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
675 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
676 path.clear();
677 path = "/";
678 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
679 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
680 #else
681 #ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000682 LOGINFO("No storage defined, defaulting to /sdcard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400683 // Standard external definition
684 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
685 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
686 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
687 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
688 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
689 #endif
690 #endif
691#endif
692
693#ifdef TW_DEFAULT_EXTERNAL_STORAGE
694 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000695 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400696#endif
697
Dees_Troy16b74352012-11-14 22:27:31 +0000698#ifdef RECOVERY_SDCARD_ON_DATA
699 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
700 SetValue(TW_INTERNAL_PATH, "/data/media/0");
701#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 str = GetCurrentStoragePath();
703#ifdef RECOVERY_SDCARD_ON_DATA
704 #ifndef TW_EXTERNAL_STORAGE_PATH
705 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
706 #else
707 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
708 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
709 } else {
710 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
711 }
712 #endif
713#else
714 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
715#endif
716 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400717
718 string dev_id;
719 GetValue("device_id", dev_id);
720
721 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400722 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
723
Dees_Troy8170a922012-09-18 15:40:25 -0400724#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000725 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400726 if (strlen(EXPAND(SP1_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400727#else
728 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000729 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Dees_Troyb46a6842012-09-25 11:06:46 -0400730 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
731 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400732#endif
733#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000734 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 if (strlen(EXPAND(SP2_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400736#else
737 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000738 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500739 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400740 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400741#endif
742#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000743 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 if (strlen(EXPAND(SP3_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400745#else
746 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000747 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500748 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400749 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400750#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400751
Dees_Troya58bead2012-09-27 09:49:29 -0400752 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400753#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100754 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
756#else
Dees_Troya58bead2012-09-27 09:49:29 -0400757 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400758#endif
Dees_Troya58bead2012-09-27 09:49:29 -0400759 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400760#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100761 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
763#else
Dees_Troya58bead2012-09-27 09:49:29 -0400764 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400765#endif
766#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000767 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
769#else
770 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
771#endif
772#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000773 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400774 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
775#else
776 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
777#endif
778#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000779 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
781#else
782 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
783#endif
784#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000785 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
787#endif
788#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000789 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
791#else
792 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
793#endif
794#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000795 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400796 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
797#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000798 char lun_file[255];
799 string Lun_File_str = CUSTOM_LUN_FILE;
800 size_t found = Lun_File_str.find("%");
801 if (found != string::npos) {
802 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
803 Lun_File_str = lun_file;
804 }
805 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000806 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000807 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
808 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000809 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000810 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
811 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400812#endif
813#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000814 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400815 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
816 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
817#else
818 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
819 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
820#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400821#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000822 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400823 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
824#endif
825#ifdef TW_INCLUDE_CRYPTO
826 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000827 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400828#endif
829#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000830 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400831 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
832#else
833 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
834#endif
835
Dees_Troya13d74f2013-03-24 08:54:55 -0500836#ifdef TW_HAS_NO_BOOT_PARTITION
837 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 0)));
838#else
839 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 0)));
840#endif
841 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees_Troy51a0e822012-09-05 15:24:24 -0400842 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0)));
843 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
844 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
845 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
846 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
847 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
848 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
849 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
850 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
851 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
852 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
853 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
854 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
855 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
856 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
857 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
858 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
859 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
860 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
861 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
862 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
863 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500864
Dees_Troy51a0e822012-09-05 15:24:24 -0400865 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
866 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
867 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
868 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
869 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400870 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
871 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
872 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
873 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
874 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
875 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
876 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
877 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
878 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
879 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
880 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
881 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
882 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
883 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
884 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
885 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
886 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
887 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
888 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
889 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500890 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
891 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400892 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
893 SetValue(TW_HAS_USB_STORAGE, 0, 0);
894 else
895 SetValue(TW_HAS_USB_STORAGE, 1, 0);
896 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
897 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
898 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
899 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
900 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
901 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
902 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
903 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
904 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
905 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
906 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
907 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500908 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600909 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Dees_Troy6ef66352013-02-21 08:26:57 -0600910 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200911#ifdef TW_BRIGHTNESS_PATH
912#ifndef TW_MAX_BRIGHTNESS
913#define TW_MAX_BRIGHTNESS 255
914#endif
Dees_Troy2f9117a2013-02-17 19:52:09 -0600915 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000916 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", EXPAND(TW_BRIGHTNESS_PATH));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600917 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
918 mConstValues.insert(make_pair("tw_brightness_file", EXPAND(TW_BRIGHTNESS_PATH)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200919 ostringstream maxVal;
920 maxVal << TW_MAX_BRIGHTNESS;
921 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
922 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
923 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600924 } else {
925 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
926 }
927#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500928 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400929}
930
931// Magic Values
932int DataManager::GetMagicValue(const string varName, string& value)
933{
934 // Handle special dynamic cases
935 if (varName == "tw_time")
936 {
937 char tmp[32];
938
939 struct tm *current;
940 time_t now;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500941 int tw_military_time;
Dees_Troy51a0e822012-09-05 15:24:24 -0400942 now = time(0);
943 current = localtime(&now);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500944 GetValue(TW_MILITARY_TIME, tw_military_time);
945 if (current->tm_hour >= 12) {
946 if (tw_military_time == 1)
947 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
948 else
949 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
950 }
951 else {
952 if (tw_military_time == 1)
953 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
954 else
955 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
956 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400957 value = tmp;
958 return 0;
959 }
960 if (varName == "tw_battery")
961 {
962 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400963 static char charging = ' ';
964 static int lastVal = -1;
965 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400966
Dees_Troy38bd7602012-09-14 13:33:53 -0400967 struct timeval curTime;
968 gettimeofday(&curTime, NULL);
969 if (curTime.tv_sec > nextSecCheck)
970 {
971 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000972#ifdef TW_CUSTOM_BATTERY_PATH
973 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
974 capacity_file += "/capacity";
975 FILE * cap = fopen(capacity_file.c_str(),"rt");
976#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400977 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000978#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 if (cap){
980 fgets(cap_s, 4, cap);
981 fclose(cap);
982 lastVal = atoi(cap_s);
983 if (lastVal > 100) lastVal = 101;
984 if (lastVal < 0) lastVal = 0;
985 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000986#ifdef TW_CUSTOM_BATTERY_PATH
987 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
988 status_file += "/status";
989 cap = fopen(status_file.c_str(),"rt");
990#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400991 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000992#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400993 if (cap) {
994 fgets(cap_s, 2, cap);
995 fclose(cap);
996 if (cap_s[0] == 'C')
997 charging = '+';
998 else
999 charging = ' ';
1000 }
1001 nextSecCheck = curTime.tv_sec + 60;
1002 }
1003
1004 sprintf(tmp, "%i%%%c", lastVal, charging);
Dees_Troy51a0e822012-09-05 15:24:24 -04001005 value = tmp;
1006 return 0;
1007 }
1008 return -1;
1009}
1010
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001011void DataManager::Output_Version(void) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001012 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001013 char version[255];
1014
Dees_Troy1c1ac442013-01-17 21:42:14 +00001015 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001016 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001017 return;
1018 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001019 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001021 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001023 return;
1024 }
1025 }
1026 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001027 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001028 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001029 }
1030 FILE *fp = fopen(Path.c_str(), "w");
1031 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001032 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001033 return;
1034 }
1035 strcpy(version, TW_VERSION_STR);
1036 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1037 fclose(fp);
1038 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001039 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001040}
1041
Dees_Troy51a0e822012-09-05 15:24:24 -04001042void DataManager::ReadSettingsFile(void)
1043{
1044 // Load up the values for TWRP - Sleep to let the card be ready
1045 char mkdir_path[255], settings_file[255];
1046 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1047
1048 GetValue(TW_IS_ENCRYPTED, is_enc);
1049 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1050 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001051 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001052 return;
1053 }
1054
1055 memset(mkdir_path, 0, sizeof(mkdir_path));
1056 memset(settings_file, 0, sizeof(settings_file));
1057 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1058 sprintf(settings_file, "%s/.twrps", mkdir_path);
1059
Dees_Troy5bf43922012-09-07 16:07:55 -04001060 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001061 {
1062 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001063 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001065 }
1066
1067 mkdir(mkdir_path, 0777);
1068
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001070 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001071 Output_Version();
Dees_Troy51a0e822012-09-05 15:24:24 -04001072 GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1073 GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
1074 GetValue(TW_HAS_EXTERNAL, has_ext);
1075 if (has_dual != 0 && use_ext == 1) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001076 // Attempt to switch to using external storage
Dees_Troye2920fa2012-09-19 16:18:00 -04001077 if (!PartitionManager.Mount_Current_Storage(false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001078 LOGERR("Failed to mount external storage, using internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001079 // Remount failed, default back to internal storage
1080 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy5bf43922012-09-07 16:07:55 -04001081 PartitionManager.Mount_Current_Storage(true);
Dees_Troye2920fa2012-09-19 16:18:00 -04001082 string int_zip_path;
1083 GetValue(TW_ZIP_INTERNAL_VAR, int_zip_path);
1084 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1085 SetValue(TW_ZIP_LOCATION_VAR, int_zip_path);
Dees_Troy51a0e822012-09-05 15:24:24 -04001086 }
1087 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001088 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001089 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001090
Dees_Troy51a0e822012-09-05 15:24:24 -04001091 if (has_ext) {
1092 string ext_path;
1093
1094 GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy5bf43922012-09-07 16:07:55 -04001095 PartitionManager.Mount_By_Path(ext_path, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001096 }
Dees_Troy8170a922012-09-18 15:40:25 -04001097 update_tz_environment_variables();
Dees_Troy2f9117a2013-02-17 19:52:09 -06001098#ifdef TW_MAX_BRIGHTNESS
1099 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
1100 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
1101 string brightness_value = GetStrValue("tw_brightness");
Dees_Troy2f9117a2013-02-17 19:52:09 -06001102 TWFunc::write_file(brightness_path, brightness_value);
1103 }
1104#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001105}
1106
1107string DataManager::GetCurrentStoragePath(void)
1108{
Dees_Troya13d74f2013-03-24 08:54:55 -05001109 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001110}
1111
1112string& DataManager::CGetCurrentStoragePath()
1113{
Dees_Troya13d74f2013-03-24 08:54:55 -05001114 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001115}
1116
1117string DataManager::GetSettingsStoragePath(void)
1118{
Dees_Troya13d74f2013-03-24 08:54:55 -05001119 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001120}
1121
1122string& DataManager::CGetSettingsStoragePath()
1123{
Dees_Troya13d74f2013-03-24 08:54:55 -05001124 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001125}
1126
1127extern "C" int DataManager_ResetDefaults()
1128{
1129 return DataManager::ResetDefaults();
1130}
1131
1132extern "C" void DataManager_LoadDefaults()
1133{
1134 return DataManager::SetDefaultValues();
1135}
1136
1137extern "C" int DataManager_LoadValues(const char* filename)
1138{
1139 return DataManager::LoadValues(filename);
1140}
1141
1142extern "C" int DataManager_Flush()
1143{
1144 return DataManager::Flush();
1145}
1146
1147extern "C" int DataManager_GetValue(const char* varName, char* value)
1148{
1149 int ret;
1150 string str;
1151
1152 ret = DataManager::GetValue(varName, str);
1153 if (ret == 0)
1154 strcpy(value, str.c_str());
1155 return ret;
1156}
1157
1158extern "C" const char* DataManager_GetStrValue(const char* varName)
1159{
1160 string& str = DataManager::GetValueRef(varName);
1161 return str.c_str();
1162}
1163
1164extern "C" const char* DataManager_GetCurrentStoragePath(void)
1165{
1166 string& str = DataManager::CGetCurrentStoragePath();
1167 return str.c_str();
1168}
1169
1170extern "C" const char* DataManager_GetSettingsStoragePath(void)
1171{
1172 string& str = DataManager::CGetSettingsStoragePath();
1173 return str.c_str();
1174}
1175
Dees_Troy51a0e822012-09-05 15:24:24 -04001176extern "C" int DataManager_GetIntValue(const char* varName)
1177{
1178 return DataManager::GetIntValue(varName);
1179}
1180
1181extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1182{
1183 return DataManager::SetValue(varName, value, 0);
1184}
1185
1186extern "C" int DataManager_SetIntValue(const char* varName, int value)
1187{
1188 return DataManager::SetValue(varName, value, 0);
1189}
1190
1191extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1192{
1193 return DataManager::SetValue(varName, value, 0);
1194}
1195
1196extern "C" int DataManager_ToggleIntValue(const char* varName)
1197{
1198 if (DataManager::GetIntValue(varName))
1199 return DataManager::SetValue(varName, 0);
1200 else
1201 return DataManager::SetValue(varName, 1);
1202}
1203
1204extern "C" void DataManager_DumpValues()
1205{
1206 return DataManager::DumpValues();
1207}
1208
1209extern "C" void DataManager_ReadSettingsFile()
1210{
1211 return DataManager::ReadSettingsFile();
1212}