文件 : mt_devs.c
__init int mt_board_init(void)
{
uint64_t key;
#if defined(CONFIG_MTK_USB_UNIQUE_SERIAL) || defined(MTK_SEC_FASTBOOT_UNLOCK_SUPPORT) //使用serial number或者使用 "0123456789ABCDEF" 开、关配置
key = get_devinfo_with_index(13);
key = (key << 32) | get_devinfo_with_index(12);
#else
key = 0;
#endif
if (key != 0)
get_serial(key, get_chip_code(), serial_number); //获取cpu chip id
else
memcpy(serial_number, "0123456789ABCDEF", 16);
retval = kobject_init_and_add(&sn_kobj, &sn_ktype, NULL, "sys_info"); //创建/sys/sys_info/serial_number接口供上层访问获取serial num
if (retval < 0)
printk("[%s] fail to add kobject\n", "sys_info");
}
/* ========================================================================= */
/* implementation of serial number attribute */
/* ========================================================================= */
#define to_sysinfo_attribute(x) container_of(x, struct sysinfo_attribute, attr)
struct sysinfo_attribute{
struct attribute attr;
ssize_t (*show)(char *buf);
ssize_t (*store)(const char *buf, size_t count);
};
static struct kobject sn_kobj;
static ssize_t sn_show(char *buf){
return snprintf(buf, 4096, "%s\n", serial_number);
}
struct sysinfo_attribute sn_attr = {
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
.attr = {"serial_number", THIS_MODULE, 0644},
#else
.attr = {"serial_number", 0644},
#endif
.show = sn_show,
.store = NULL
};
static ssize_t sysinfo_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct sysinfo_attribute *sysinfo_attr = to_sysinfo_attribute(attr);
ssize_t ret = -EIO;
if(sysinfo_attr->show)
ret = sysinfo_attr->show(buf);
return ret;
}
static ssize_t sysinfo_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count)
{
struct sysinfo_attribute *sysinfo_attr = to_sysinfo_attribute(attr);
ssize_t ret = -EIO;
if(sysinfo_attr->store)
ret = sysinfo_attr->store(buf, count);
return ret;
}
static struct sysfs_ops sn_sysfs_ops = {
.show = sysinfo_show,
.store = sysinfo_store
};
static struct attribute *sn_attrs[] = {
&sn_attr.attr,
NULL
};
static struct kobj_type sn_ktype = {
.sysfs_ops = &sn_sysfs_ops,
.default_attrs = sn_attrs
};
chown system system /sys/class/android_usb/android0/iSerial
修改权限后,可以通过下面sys修改sn
/system/core/init/init.c
static int read_serialno()
{
char *pval;
int fd;
char serialno[32];
size_t s;
pval = property_get("ro.boot.serialno");
if (pval) {
NOTICE("Already get serial number from cmdline\n");
return 1;
}ro.boot.serialno
fd = open("/sys/sys_info/serial_number", O_RDWR); //从底层接口/sys/sys_info/serial_number读取serial number
if (fd < 0) {
xlog_printf(ANDROID_LOG_WARN, LOG_TAG, "fail to open: %s\n", "/sys/sys_info/serial_number");
return 0;
}
s = read(fd, serialno, sizeof(char)*32);
serialno[s-1] = '\0';
close(fd);
if (s <= 0) {
xlog_printf(ANDROID_LOG_WARN, LOG_TAG, "could not read serial number sys file\n");
return 0;
}
xlog_printf(ANDROID_LOG_WARN, LOG_TAG, "serial number=%s\n",serialno);
property_set("ro.boot.serialno", serialno); //serial number 设置到"ro.boot.serialno
return 1;
}
static void export_kernel_boot_props(void)
{
char tmp[PROP_VALUE_MAX];
const char *pval;
unsigned i;
struct {
const char *src_prop;
const char *dest_prop;
const char *def_val;
} prop_map[] = {
{ "ro.boot.serialno", "ro.serialno", "0123456789ABCDEF", },
};
for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
pval = property_get(prop_map[i].src_prop);
property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val); //ro.boot.serialno 设置到ro.serialno
}
/** A hardware serial number, if available. Alphanumeric only, case-insensitive. */
public static final String SERIAL = getString("ro.serialno"); //android.os.Build.SERIA =ro.serialno
..............
}