Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Persistent Data Storage in Android 12 Using MTK NVRAM

Tech Jul 6 2

Overview

During a recent project, a client requested a feature to store a block of data persistent on a MediaTek (MTK) Android 12 device, such that the data remains intact even after a factory reset. This requirement led to the use of NVRAM (Non-Volatile Random Access Memory), a MediaTek-specific storage mechanism that supports persistent data storage outside the standard Android filesystem. Custom NVRAM Structure Setup

To store custom data in NVRAM, a new structure must be defined and registered within the MediaTek codebase. Below are the necessary steps and code snippets to create a new NVRAM structure for storing 1024 bytes of custom data. ### 1. Define the Data Structure

The structure is defined in the configuraton header file: ```

typedef struct { unsigned char Data[1024]; } CustomDataStructure;

#define CFG_CUSTOM_DATA_REC_SIZE sizeof(CustomDataStructure) #define CFG_CUSTOM_DATA_REC_TOTAL 1


### 2. Set Default Values

A default value file is created to initialize the structure: ```

CustomDataStructure stCustomDataDefault =
{
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};

3. Register the NVRAM ID

Add a new ID to the list of NVRAM file identifiers: ```

typedef enum { AP_CFG_RDCL_FILE_AUDIO_BT_GAIN_CUSTOM_LID, AP_CFG_RDCL_FILE_AUDIO_FUNC_SWITCH_PARAM_LID, AP_CFG_CUSTOM_FILE_FUEL_GAUGE_LID, AP_CFG_CUSTOM_FILE_MY_DATA_LID, AP_CFG_CUSTOM_FILE_MAX_LID, } CUSTOM_CFG_FILE_LID;

#define AP_CFG_CUSTOM_FILE_MY_DATA_LID_VERNO "000"


### 4. Declare the Data Item

Register the structure in the NVRAM data item definitions: ```

LID_BIT VER_LID(AP_CFG_CUSTOM_FILE_MY_DATA_LID)
CustomDataStructure *CFG_CUSTOM_DATA_REC_TOTAL
{
};

5. Add Entry to NVRAM File List

Finally, the new entry is added to the NVRAM file array: ```

const TCFG_FILE g_akCFG_File_Custom[] = { ... { "/mnt/vendor/protect_f/MY_DATA", VER(AP_CFG_CUSTOM_FILE_MY_DATA_LID), CFG_CUSTOM_DATA_REC_SIZE, CFG_CUSTOM_DATA_REC_TOTAL, SINGLE_DEFAULT_REC, (char *)&stCustomDataDefault, DataReset, NULL }, };


Implementing Read/Write Access
------------------------------

Once the structure is defined, the next step is to implement read and write operations. A Java class is created to encapsulate NVRAM access and can be packaged as a JAR for reuse across applications. ### Java Manager Class

Here is a simplified version of the manager class: ```

package com.mediatek.settings.test;

import android.os.ServiceManager;
import android.os.IBinder;
import android.util.Log;

public class CustomDataManager {
    private static final String TAG = "CustomDataManager";

    private IBinder mBinder = ServiceManager.getService("custom_data_service");

    public boolean writeData(byte[] data) {
        if (data.length > 1024) {
            Log.e(TAG, "Data too large");
            return false;
        }
        // Call native method to write to NVRAM
        return nativeWriteData(data, data.length);
    }

    public byte[] readData() {
        byte[] data = nativeReadData();
        return data;
    }

    private native boolean nativeWriteData(byte[] data, int length);
    private native byte[] nativeReadData();
}

This class uses Binder to communicate with a system-level service and includes native methods for interacting with NVRAM. It can be extended with error handling, data validation, and synchronization logic depending on the use case. Conclusion

By leveraging MediaTek's NVRAM subsystem, it's possible to implement persistent data storage that survives factory resets. This approach is particularly useful for device-specific configuration or critical user data that needs to persist beyond the normal lifecycle of app or system data.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.