blob: f8b19dd58ae9f57cdbdee34727427dd03052beb2 [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_Troy51a0e822012-09-05 15:24:24 -040043
44extern "C"
45{
46 #include "common.h"
47 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040048 #include "gui/pages.h"
49
50 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040051}
52
53#define FILE_VERSION 0x00010001
54
55using namespace std;
56
57map<string, DataManager::TStrIntPair> DataManager::mValues;
58map<string, string> DataManager::mConstValues;
59string DataManager::mBackingFile;
60int DataManager::mInitialized = 0;
61
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040062// Device ID functions
63void DataManager::sanitize_device_id(char* device_id) {
64 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
65 char str[50];
66 char* c = str;
67
68 strcpy(str, device_id);
69 memset(device_id, 0, sizeof(device_id));
70 while (*c) {
71 if (strchr(whitelist, *c))
72 strncat(device_id, c, 1);
73 c++;
74 }
75 return;
76}
77
78#define CMDLINE_SERIALNO "androidboot.serialno="
79#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
80#define CPUINFO_SERIALNO "Serial"
81#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
82#define CPUINFO_HARDWARE "Hardware"
83#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
84
85void DataManager::get_device_id(void) {
86 FILE *fp;
87 char line[2048];
88 char hardware_id[32], device_id[64];
89 char* token;
90
91 // Assign a blank device_id to start with
92 device_id[0] = 0;
93#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
94 // First, try the cmdline to see if the serial number was supplied
95 fp = fopen("/proc/cmdline", "rt");
96 if (fp != NULL)
97 {
98 // First step, read the line. For cmdline, it's one long line
99 fgets(line, sizeof(line), fp);
100 fclose(fp);
101
102 // Now, let's tokenize the string
103 token = strtok(line, " ");
104
105 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
106 while (token)
107 {
108 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
109 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
110 {
111 // We found the serial number!
112 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
113 sanitize_device_id((char *)device_id);
114 mConstValues.insert(make_pair("device_id", device_id));
115 return;
116 }
117 token = strtok(NULL, " ");
118 }
119 }
120#endif
121 // Now we'll try cpuinfo for a serial number
122 fp = fopen("/proc/cpuinfo", "rt");
123 if (fp != NULL)
124 {
125 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
126 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
127 {
128 // We found the serial number!
129 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
130 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
131 if (*token != 0) {
132 token[30] = 0;
133 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
134 memset(device_id, 0, sizeof(device_id));
135 strncpy(device_id, token, strlen(token) - 1);
136 } else {
137 strcpy(device_id, token);
138 }
139 LOGI("=> serial from cpuinfo: '%s'\n", device_id);
140 fclose(fp);
141 sanitize_device_id((char *)device_id);
142 mConstValues.insert(make_pair("device_id", device_id));
143 return;
144 }
145 } 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
146 // We found the hardware ID
147 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
148 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
149 if (*token != 0) {
150 token[30] = 0;
151 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
152 memset(hardware_id, 0, sizeof(hardware_id));
153 strncpy(hardware_id, token, strlen(token) - 1);
154 } else {
155 strcpy(hardware_id, token);
156 }
157 LOGI("=> hardware id from cpuinfo: '%s'\n", hardware_id);
158 }
159 }
160 }
161 fclose(fp);
162 }
163
164 if (hardware_id[0] != 0) {
165 LOGW("\nusing hardware id for device id: '%s'\n", hardware_id);
166 strcpy(device_id, hardware_id);
167 sanitize_device_id((char *)device_id);
168 mConstValues.insert(make_pair("device_id", device_id));
169 return;
170 }
171
172 strcpy(device_id, "serialno");
173 LOGE("=> device id not found, using '%s'.", device_id);
174 mConstValues.insert(make_pair("device_id", device_id));
175 return;
176}
177
Dees_Troy51a0e822012-09-05 15:24:24 -0400178int DataManager::ResetDefaults()
179{
180 mValues.clear();
181 mConstValues.clear();
182 SetDefaultValues();
183 return 0;
184}
185
186int DataManager::LoadValues(const string filename)
187{
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400188 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189
190 if (!mInitialized)
191 SetDefaultValues();
192
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400193 GetValue("device_id", dev_id);
194 // Save off the backing file for set operations
Dees_Troy51a0e822012-09-05 15:24:24 -0400195 mBackingFile = filename;
196
197 // Read in the file, if possible
198 FILE* in = fopen(filename.c_str(), "rb");
199 if (!in) return 0;
200
201 int file_version;
202 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
203 if (file_version != FILE_VERSION) goto error;
204
205 while (!feof(in))
206 {
207 string Name;
208 string Value;
209 unsigned short length;
210 char array[512];
211
212 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
213 if (length >= 512) goto error;
214 if (fread(array, 1, length, in) != length) goto error;
215 Name = array;
216
217 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
218 if (length >= 512) goto error;
219 if (fread(array, 1, length, in) != length) goto error;
220 Value = array;
221
222 map<string, TStrIntPair>::iterator pos;
223
224 pos = mValues.find(Name);
225 if (pos != mValues.end())
226 {
227 pos->second.first = Value;
228 pos->second.second = 1;
229 }
230 else
231 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
232 }
233 fclose(in);
234
235 str = GetCurrentStoragePath();
236 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400237 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400238 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
239
240 return 0;
241
242error:
243 // File version mismatch. Use defaults.
244 fclose(in);
245 str = GetCurrentStoragePath();
246 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400247 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
249 return -1;
250}
251
252int DataManager::Flush()
253{
254 return SaveValues();
255}
256
257int DataManager::SaveValues()
258{
259 if (mBackingFile.empty()) return -1;
260
261 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
264 FILE* out = fopen(mBackingFile.c_str(), "wb");
265 if (!out) return -1;
266
267 int file_version = FILE_VERSION;
268 fwrite(&file_version, 1, sizeof(int), out);
269
270 map<string, TStrIntPair>::iterator iter;
271 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
272 {
273 // Save only the persisted data
274 if (iter->second.second != 0)
275 {
276 unsigned short length = (unsigned short) iter->first.length() + 1;
277 fwrite(&length, 1, sizeof(unsigned short), out);
278 fwrite(iter->first.c_str(), 1, length, out);
279 length = (unsigned short) iter->second.first.length() + 1;
280 fwrite(&length, 1, sizeof(unsigned short), out);
281 fwrite(iter->second.first.c_str(), 1, length, out);
282 }
283 }
284 fclose(out);
285 return 0;
286}
287
288int DataManager::GetValue(const string varName, string& value)
289{
290 string localStr = varName;
291
292 if (!mInitialized)
293 SetDefaultValues();
294
295 // Strip off leading and trailing '%' if provided
296 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
297 {
298 localStr.erase(0, 1);
299 localStr.erase(localStr.length() - 1, 1);
300 }
301
302 // Handle magic values
303 if (GetMagicValue(localStr, value) == 0) return 0;
304
305 map<string, string>::iterator constPos;
306 constPos = mConstValues.find(localStr);
307 if (constPos != mConstValues.end())
308 {
309 value = constPos->second;
310 return 0;
311 }
312
313 map<string, TStrIntPair>::iterator pos;
314 pos = mValues.find(localStr);
315 if (pos == mValues.end())
316 return -1;
317
318 value = pos->second.first;
319 return 0;
320}
321
322int DataManager::GetValue(const string varName, int& value)
323{
324 string data;
325
326 if (GetValue(varName,data) != 0)
327 return -1;
328
329 value = atoi(data.c_str());
330 return 0;
331}
332
333// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
334string& DataManager::GetValueRef(const string varName)
335{
336 if (!mInitialized)
337 SetDefaultValues();
338
339 map<string, string>::iterator constPos;
340 constPos = mConstValues.find(varName);
341 if (constPos != mConstValues.end())
342 return constPos->second;
343
344 map<string, TStrIntPair>::iterator pos;
345 pos = mValues.find(varName);
346 if (pos == mValues.end())
347 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
348
349 return pos->second.first;
350}
351
352// This function will return an empty string if the value doesn't exist
353string DataManager::GetStrValue(const string varName)
354{
355 string retVal;
356
357 GetValue(varName, retVal);
358 return retVal;
359}
360
361// This function will return 0 if the value doesn't exist
362int DataManager::GetIntValue(const string varName)
363{
364 string retVal;
365
366 GetValue(varName, retVal);
367 return atoi(retVal.c_str());
368}
369
370int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
371{
372 if (!mInitialized)
373 SetDefaultValues();
374
375 // Don't allow empty values or numerical starting values
376 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
377 return -1;
378
379 map<string, string>::iterator constChk;
380 constChk = mConstValues.find(varName);
381 if (constChk != mConstValues.end())
382 return -1;
383
384 map<string, TStrIntPair>::iterator pos;
385 pos = mValues.find(varName);
386 if (pos == mValues.end())
387 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
388 else
389 pos->second.first = value;
390
391 if (pos->second.second != 0)
392 SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400393 gui_notifyVarChange(varName.c_str(), value.c_str());
394 return 0;
395}
396
397int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
398{
399 ostringstream valStr;
400 valStr << value;
401 if (varName == "tw_use_external_storage") {
402 string str;
403
404 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
405 if (value == 0) {
406 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400407 } else {
408 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 }
410 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
411 str = GetStrValue(TW_INTERNAL_PATH);
412 else
413 str = GetStrValue(TW_EXTERNAL_PATH);
414
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400415 string dev_id;
416
417 GetValue("device_id", dev_id);
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400419 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420
421 SetValue(TW_BACKUPS_FOLDER_VAR, str);
422 }
423 return SetValue(varName, valStr.str(), persist);;
424}
425
426int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
427{
428 ostringstream valStr;
429 valStr << value;
430 return SetValue(varName, valStr.str(), persist);;
431}
432
433void DataManager::DumpValues()
434{
435 map<string, TStrIntPair>::iterator iter;
436 ui_print("Data Manager dump - Values with leading X are persisted.\n");
437 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
438 {
439 ui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
440 }
441}
442
Dees_Troy8170a922012-09-18 15:40:25 -0400443void DataManager::update_tz_environment_variables(void) {
444 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
445 tzset();
446}
447
Dees_Troy16b74352012-11-14 22:27:31 +0000448void DataManager::SetBackupFolder()
449{
450 string str = GetCurrentStoragePath();
451 str += "/TWRP/BACKUPS/";
452
453 string dev_id;
454 GetValue("device_id", dev_id);
455
456 str += dev_id;
457 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
458}
459
Dees_Troy51a0e822012-09-05 15:24:24 -0400460void DataManager::SetDefaultValues()
461{
462 string str, path;
463
464 get_device_id();
465
466 mInitialized = 1;
467
468 mConstValues.insert(make_pair("true", "1"));
469 mConstValues.insert(make_pair("false", "0"));
470
471 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
472
Dees_Troyf4499812013-01-23 19:07:38 +0000473#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
474 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
475#endif
476
Dees_Troy51a0e822012-09-05 15:24:24 -0400477#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000478 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
480#else
481 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
482#endif
483
484#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000485 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
487#else
488 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
489#endif
490
491#ifdef TW_INTERNAL_STORAGE_PATH
492 LOGI("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
493 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
494 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000495 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
497 path.clear();
498 path = "/";
499 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
500 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
501 #ifdef TW_EXTERNAL_STORAGE_PATH
502 LOGI("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
503 // Device has dual storage
504 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
505 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
506 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
507 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
508 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
509 path.clear();
510 path = "/";
511 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
512 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
513 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
514 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
515 } else {
516 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
517 }
518 #else
519 LOGI("Just has internal storage.\n");
520 // Just has internal storage
521 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
522 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
523 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
524 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
525 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
526 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
527 #endif
528#else
529 #ifdef RECOVERY_SDCARD_ON_DATA
530 #ifdef TW_EXTERNAL_STORAGE_PATH
531 LOGI("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
532 // Device has /data/media + external storage
533 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
534 #else
535 LOGI("Single storage only -- data/media.\n");
536 // Device just has external storage
537 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
538 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
539 #endif
540 #else
541 LOGI("Single storage only.\n");
542 // Device just has external storage
543 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
544 #endif
545 #ifdef RECOVERY_SDCARD_ON_DATA
546 LOGI("Device has /data/media defined.\n");
547 // Device has /data/media
548 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
549 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000550 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400551 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
552 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
553 #ifdef TW_EXTERNAL_STORAGE_PATH
554 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
555 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
556 } else {
557 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
558 }
559 #else
560 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
561 #endif
562 #else
563 LOGI("No internal storage defined.\n");
564 // Device has no internal storage
565 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
566 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000567 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400568 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
569 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
570 #endif
571 #ifdef TW_EXTERNAL_STORAGE_PATH
572 LOGI("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
573 // External has custom definition
574 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
575 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
576 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
577 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
578 path.clear();
579 path = "/";
580 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
581 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
582 #else
583 #ifndef RECOVERY_SDCARD_ON_DATA
584 LOGI("No storage defined, defaulting to /sdcard.\n");
585 // Standard external definition
586 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
587 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
588 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
589 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
590 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
591 #endif
592 #endif
593#endif
594
595#ifdef TW_DEFAULT_EXTERNAL_STORAGE
596 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000597 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400598#endif
599
Dees_Troy16b74352012-11-14 22:27:31 +0000600#ifdef RECOVERY_SDCARD_ON_DATA
601 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
602 SetValue(TW_INTERNAL_PATH, "/data/media/0");
603#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400604 str = GetCurrentStoragePath();
605#ifdef RECOVERY_SDCARD_ON_DATA
606 #ifndef TW_EXTERNAL_STORAGE_PATH
607 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
608 #else
609 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
610 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
611 } else {
612 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
613 }
614 #endif
615#else
616 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
617#endif
618 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400619
620 string dev_id;
621 GetValue("device_id", dev_id);
622
623 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400624 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
625
Dees_Troy8170a922012-09-18 15:40:25 -0400626#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000627 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 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 -0400629#else
630 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000631 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Dees_Troyb46a6842012-09-25 11:06:46 -0400632 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
633 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400634#endif
635#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000636 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400637 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 -0400638#else
639 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000640 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500641 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400642 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400643#endif
644#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000645 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400646 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 -0400647#else
648 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000649 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500650 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400651 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400652#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400653
Dees_Troya58bead2012-09-27 09:49:29 -0400654 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400655#ifdef TW_NO_REBOOT_RECOVERY
Dees_Troyf4499812013-01-23 19:07:38 +0000656 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400657 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
658#else
Dees_Troya58bead2012-09-27 09:49:29 -0400659 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400660#endif
Dees_Troya58bead2012-09-27 09:49:29 -0400661 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400662#ifdef TW_NO_REBOOT_BOOTLOADER
Dees_Troyf4499812013-01-23 19:07:38 +0000663 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
665#else
Dees_Troya58bead2012-09-27 09:49:29 -0400666 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400667#endif
668#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000669 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
671#else
672 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
673#endif
674#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000675 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
677#else
678 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
679#endif
680#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000681 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400682 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
683#else
684 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
685#endif
686#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000687 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400688 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
689#endif
690#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000691 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
693#else
694 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
695#endif
696#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000697 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
699#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000700 char lun_file[255];
701 string Lun_File_str = CUSTOM_LUN_FILE;
702 size_t found = Lun_File_str.find("%");
703 if (found != string::npos) {
704 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
705 Lun_File_str = lun_file;
706 }
707 if (!TWFunc::Path_Exists(Lun_File_str)) {
708 LOGI("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
709 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
710 } else {
711 LOGI("Lun file '%s'\n", Lun_File_str.c_str());
712 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
713 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400714#endif
715#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000716 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
718 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
719#else
720 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
721 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
722#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400723#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000724 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
726#endif
727#ifdef TW_INCLUDE_CRYPTO
728 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000729 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400730#endif
731#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000732 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
734#else
735 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
736#endif
737
738 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
739 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0)));
740 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
741 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
742 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
743 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
744 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
745 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
746 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
747 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
748 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
749 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
750 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
751 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
752 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
753 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
754 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
755 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
756 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
757 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
758 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
759 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
760 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
761
762 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
763 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
764 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
765 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
766 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
767 mValues.insert(make_pair(TW_IGNORE_IMAGE_SIZE, make_pair("0", 1)));
768 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
769 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
770 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
771 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
772 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
773 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
774 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
775 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
776 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
777 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
778 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
779 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
780 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
781 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
782 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
783 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
784 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
785 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
786 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
787 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
788 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
789 SetValue(TW_HAS_USB_STORAGE, 0, 0);
790 else
791 SetValue(TW_HAS_USB_STORAGE, 1, 0);
792 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
793 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
794 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
795 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
796 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
797 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
798 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
799 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
800 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
801 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
802 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
803 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
804}
805
806// Magic Values
807int DataManager::GetMagicValue(const string varName, string& value)
808{
809 // Handle special dynamic cases
810 if (varName == "tw_time")
811 {
812 char tmp[32];
813
814 struct tm *current;
815 time_t now;
816 now = time(0);
817 current = localtime(&now);
818
819 if (current->tm_hour >= 12)
820 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
821 else
822 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
823
824 value = tmp;
825 return 0;
826 }
827 if (varName == "tw_battery")
828 {
829 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400830 static char charging = ' ';
831 static int lastVal = -1;
832 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400833
Dees_Troy38bd7602012-09-14 13:33:53 -0400834 struct timeval curTime;
835 gettimeofday(&curTime, NULL);
836 if (curTime.tv_sec > nextSecCheck)
837 {
838 char cap_s[4];
839 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
840 if (cap){
841 fgets(cap_s, 4, cap);
842 fclose(cap);
843 lastVal = atoi(cap_s);
844 if (lastVal > 100) lastVal = 101;
845 if (lastVal < 0) lastVal = 0;
846 }
847 cap = fopen("/sys/class/power_supply/battery/status","rt");
848 if (cap) {
849 fgets(cap_s, 2, cap);
850 fclose(cap);
851 if (cap_s[0] == 'C')
852 charging = '+';
853 else
854 charging = ' ';
855 }
856 nextSecCheck = curTime.tv_sec + 60;
857 }
858
859 sprintf(tmp, "%i%%%c", lastVal, charging);
Dees_Troy51a0e822012-09-05 15:24:24 -0400860 value = tmp;
861 return 0;
862 }
863 return -1;
864}
865
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400866void DataManager::Output_Version(void) {
Dees_Troy1c1ac442013-01-17 21:42:14 +0000867 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400868 char version[255];
869
Dees_Troy1c1ac442013-01-17 21:42:14 +0000870 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400871 LOGI("Unable to mount '%s' to write version number.\n", Path.c_str());
872 return;
873 }
Dees_Troy1c1ac442013-01-17 21:42:14 +0000874 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
875 LOGI("Recreating /cache/recovery folder.\n");
876 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
877 LOGE("DataManager::Output_Version -- Unable to make /cache/recovery\n");
878 return;
879 }
880 }
881 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400882 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500883 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400884 }
885 FILE *fp = fopen(Path.c_str(), "w");
886 if (fp == NULL) {
887 LOGE("Unable to open '%s'.\n", Path.c_str());
888 return;
889 }
890 strcpy(version, TW_VERSION_STR);
891 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
892 fclose(fp);
893 sync();
894 LOGI("Version number saved to '%s'\n", Path.c_str());
895}
896
Dees_Troy51a0e822012-09-05 15:24:24 -0400897void DataManager::ReadSettingsFile(void)
898{
899 // Load up the values for TWRP - Sleep to let the card be ready
900 char mkdir_path[255], settings_file[255];
901 int is_enc, has_dual, use_ext, has_data_media, has_ext;
902
903 GetValue(TW_IS_ENCRYPTED, is_enc);
904 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
905 if (is_enc == 1 && has_data_media == 1) {
906 LOGI("Cannot load settings -- encrypted.\n");
907 return;
908 }
909
910 memset(mkdir_path, 0, sizeof(mkdir_path));
911 memset(settings_file, 0, sizeof(settings_file));
912 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
913 sprintf(settings_file, "%s/.twrps", mkdir_path);
914
Dees_Troy5bf43922012-09-07 16:07:55 -0400915 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400916 {
917 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -0400918 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -0400919 LOGE("Unable to mount %s when trying to read settings file.\n", DataManager_GetSettingsStorageMount());
920 }
921
922 mkdir(mkdir_path, 0777);
923
924 LOGI("Attempt to load settings from settings file...\n");
925 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400926 Output_Version();
Dees_Troy51a0e822012-09-05 15:24:24 -0400927 GetValue(TW_HAS_DUAL_STORAGE, has_dual);
928 GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
929 GetValue(TW_HAS_EXTERNAL, has_ext);
930 if (has_dual != 0 && use_ext == 1) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400931 // Attempt to switch to using external storage
Dees_Troye2920fa2012-09-19 16:18:00 -0400932 if (!PartitionManager.Mount_Current_Storage(false)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400933 LOGE("Failed to mount external storage, using internal storage.\n");
934 // Remount failed, default back to internal storage
935 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy5bf43922012-09-07 16:07:55 -0400936 PartitionManager.Mount_Current_Storage(true);
Dees_Troye2920fa2012-09-19 16:18:00 -0400937 string int_zip_path;
938 GetValue(TW_ZIP_INTERNAL_VAR, int_zip_path);
939 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
940 SetValue(TW_ZIP_LOCATION_VAR, int_zip_path);
Dees_Troy51a0e822012-09-05 15:24:24 -0400941 }
942 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400943 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400944 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400945
Dees_Troy51a0e822012-09-05 15:24:24 -0400946 if (has_ext) {
947 string ext_path;
948
949 GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400950 PartitionManager.Mount_By_Path(ext_path, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400951 }
Dees_Troy8170a922012-09-18 15:40:25 -0400952 update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400953}
954
955string DataManager::GetCurrentStoragePath(void)
956{
957 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
958 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
959 return GetStrValue(TW_INTERNAL_PATH);
960 else
961 return GetStrValue(TW_EXTERNAL_PATH);
962 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
963 return GetStrValue(TW_INTERNAL_PATH);
964 else
965 return GetStrValue(TW_EXTERNAL_PATH);
966}
967
968string& DataManager::CGetCurrentStoragePath()
969{
970 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
971 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
972 return GetValueRef(TW_INTERNAL_PATH);
973 else
974 return GetValueRef(TW_EXTERNAL_PATH);
975 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
976 return GetValueRef(TW_INTERNAL_PATH);
977 else
978 return GetValueRef(TW_EXTERNAL_PATH);
979}
980
981string DataManager::GetCurrentStorageMount(void)
982{
983 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
984 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
985 return GetStrValue(TW_INTERNAL_MOUNT);
986 else
987 return GetStrValue(TW_EXTERNAL_MOUNT);
988 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
989 return GetStrValue(TW_INTERNAL_MOUNT);
990 else
991 return GetStrValue(TW_EXTERNAL_MOUNT);
992}
993
994string& DataManager::CGetCurrentStorageMount()
995{
996 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
997 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
998 return GetValueRef(TW_INTERNAL_MOUNT);
999 else
1000 return GetValueRef(TW_EXTERNAL_MOUNT);
1001 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
1002 return GetValueRef(TW_INTERNAL_MOUNT);
1003 else
1004 return GetValueRef(TW_EXTERNAL_MOUNT);
1005}
1006
1007string DataManager::GetSettingsStoragePath(void)
1008{
1009 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1010 return GetStrValue(TW_INTERNAL_PATH);
1011 else
1012 return GetStrValue(TW_EXTERNAL_PATH);
1013}
1014
1015string& DataManager::CGetSettingsStoragePath()
1016{
1017 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1018 return GetValueRef(TW_INTERNAL_PATH);
1019 else
1020 return GetValueRef(TW_EXTERNAL_PATH);
1021}
1022
1023string DataManager::GetSettingsStorageMount(void)
1024{
1025 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1026 return GetStrValue(TW_INTERNAL_MOUNT);
1027 else
1028 return GetStrValue(TW_EXTERNAL_MOUNT);
1029}
1030
1031string& DataManager::CGetSettingsStorageMount()
1032{
1033 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1034 return GetValueRef(TW_INTERNAL_MOUNT);
1035 else
1036 return GetValueRef(TW_EXTERNAL_MOUNT);
1037}
1038
1039extern "C" int DataManager_ResetDefaults()
1040{
1041 return DataManager::ResetDefaults();
1042}
1043
1044extern "C" void DataManager_LoadDefaults()
1045{
1046 return DataManager::SetDefaultValues();
1047}
1048
1049extern "C" int DataManager_LoadValues(const char* filename)
1050{
1051 return DataManager::LoadValues(filename);
1052}
1053
1054extern "C" int DataManager_Flush()
1055{
1056 return DataManager::Flush();
1057}
1058
1059extern "C" int DataManager_GetValue(const char* varName, char* value)
1060{
1061 int ret;
1062 string str;
1063
1064 ret = DataManager::GetValue(varName, str);
1065 if (ret == 0)
1066 strcpy(value, str.c_str());
1067 return ret;
1068}
1069
1070extern "C" const char* DataManager_GetStrValue(const char* varName)
1071{
1072 string& str = DataManager::GetValueRef(varName);
1073 return str.c_str();
1074}
1075
1076extern "C" const char* DataManager_GetCurrentStoragePath(void)
1077{
1078 string& str = DataManager::CGetCurrentStoragePath();
1079 return str.c_str();
1080}
1081
1082extern "C" const char* DataManager_GetSettingsStoragePath(void)
1083{
1084 string& str = DataManager::CGetSettingsStoragePath();
1085 return str.c_str();
1086}
1087
1088extern "C" const char* DataManager_GetCurrentStorageMount(void)
1089{
1090 string& str = DataManager::CGetCurrentStorageMount();
1091 return str.c_str();
1092}
1093
1094extern "C" const char* DataManager_GetSettingsStorageMount(void)
1095{
1096 string& str = DataManager::CGetSettingsStorageMount();
1097 return str.c_str();
1098}
1099
1100extern "C" int DataManager_GetIntValue(const char* varName)
1101{
1102 return DataManager::GetIntValue(varName);
1103}
1104
1105extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1106{
1107 return DataManager::SetValue(varName, value, 0);
1108}
1109
1110extern "C" int DataManager_SetIntValue(const char* varName, int value)
1111{
1112 return DataManager::SetValue(varName, value, 0);
1113}
1114
1115extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1116{
1117 return DataManager::SetValue(varName, value, 0);
1118}
1119
1120extern "C" int DataManager_ToggleIntValue(const char* varName)
1121{
1122 if (DataManager::GetIntValue(varName))
1123 return DataManager::SetValue(varName, 0);
1124 else
1125 return DataManager::SetValue(varName, 1);
1126}
1127
1128extern "C" void DataManager_DumpValues()
1129{
1130 return DataManager::DumpValues();
1131}
1132
1133extern "C" void DataManager_ReadSettingsFile()
1134{
1135 return DataManager::ReadSettingsFile();
1136}