NVRAM讀寫方法
0.前言:有的時候業務需要讀寫NVRAM裡面的資料,比如讀MAC地址或者自定義NVRAM節點然後讀寫操作其節點值(NVRAM裡面的值恢復出廠設定後還存在)
1.實際在需要讀寫nvram的地方匯入NVRAMUtils.java和NvRAMAgent.java檔案(注意檔案的包名與新增的路徑要一致),然後用
NVRAMUtils.readNvRAMFile(NVRAM的路徑名)和 writeNvRAMFile就可以進行讀寫操作了,且讀寫的是byte陣列,實際可能需要再轉換為string型別的資料
2.實際除錯可以通過adb shell然後cd到具體的節點路徑,然後通過cat和echo讀寫裡面的值,然後也可以pull出那個檔案通過相關的軟體檢視裡面的值(實際儲存的為byte型別,檔案檢視到的為16進位制資料)
========================
NVRAMUtils.java
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.os.ServiceManager;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public static byte[] readNvRAMFile(String filename) {
IBinder binder = ServiceManager.getService("NvRAMAgent");
NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);
byte[] buff = null;
try {
buff = agent.readFileByName(filename);
return buff;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void writeNvRAMFile(String filename,byte[] buff) {
IBinder binder = ServiceManager.getService("NvRAMAgent");
NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);
try {
agent.writeFileByName(filename,buff);
Log.i("xiaozheng", "ready backupToBinRegionAll");
agent.backupToBinRegionAll();
} catch (Exception e) {
Log.i("xiaozheng", "writeNvRAMFile e="+e);
e.printStackTrace();
}
}
=======================
NvRAMAgent.java
import android.os.IBinder;
public interface NvRAMAgent extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements NvRAMAgent {
private static final java.lang.String DESCRIPTOR = "NvRAMAgent";
/** Construct the stub at attach it to the interface. */
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an NvRAMAgent interface,
* generating a proxy if needed.
* @param obj represents IBinder class.
* @return the object of NvRAMAgent.
*/
public static NvRAMAgent asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)
obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof NvRAMAgent))) {
return ((NvRAMAgent) iin);
}
return new NvRAMAgent.Stub.Proxy(obj);
}
public android.os.IBinder asBinder() {
return this;
}
public boolean onTransact(int code, android.os.Parcel data,
android.os.Parcel reply, int flags) throws android.os.RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_READFILE: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
byte[] _result = this.readFile(_arg0);
reply.writeNoException();
reply.writeByteArray(_result);
return true;
}
case TRANSACTION_WRITEFILE: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
byte[] _arg1;
_arg1 = data.createByteArray();
int _result = this.writeFile(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_backupToBinRegionAll:{
data.enforceInterface(DESCRIPTOR);
int _result = this.backupToBinRegionAll();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
default: {
break;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements NvRAMAgent
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public byte[] readFile(int file_lid) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
byte[] _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(file_lid);
mRemote.transact(Stub.TRANSACTION_READFILE, _data, _reply, 0);
_reply.readException();
_result = _reply.createByteArray();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public int writeFile(int file_lid, byte[] buff) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(file_lid);
_data.writeByteArray(buff);
mRemote.transact(Stub.TRANSACTION_WRITEFILE, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public byte[] readFileByName(String filename) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
byte[] _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(filename);
mRemote.transact(Stub.TRANSACTION_READFILEBYNAME, _data, _reply, 0);
_reply.readException();
_result = _reply.createByteArray();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public int writeFileByName(String filename, byte[] buff)
throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(filename);
_data.writeByteArray(buff);
mRemote.transact(Stub.TRANSACTION_WRITEFILEBYNAME, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public int backupToBinRegionAll()
throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_backupToBinRegionAll, _data, _reply,
0);
_reply.readException();
_result = _reply.readInt();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_READFILE = (IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_WRITEFILE = (IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_READFILEBYNAME = (IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_WRITEFILEBYNAME = (IBinder.FIRST_CALL_TRANSACTION + 3);
static final int TRANSACTION_backupToBinRegionAll = (IBinder.FIRST_CALL_TRANSACTION + 4);
}
public byte[] readFile(int file_lid) throws android.os.RemoteException;
public int writeFile(int file_lid, byte[] buff) throws android.os.RemoteException;
public byte[] readFileByName(String filepath) throws android.os.RemoteException;
public int writeFileByName(String filepath, byte[] buff) throws android.os.RemoteException;
public int backupToBinRegionAll() throws android.os.RemoteException;
}