本文共 5230 字,大约阅读时间需要 17 分钟。
最近在一些项目里接触到了hw笔记本+linux,hw在笔记本上自己做了一个linux内核原本没有驱动——bootdevice。研究了一下发现,这好像是从安卓的内核里面扣过来的,不清楚是安卓内核本身就有还是hw自己做的。github上找到一个hw安卓内核分析了一下,感觉还挺有意思的,分享一下。
bootdevice驱动写的非常加单,是用来记录启动设备的驱动,这是驱动的说明Support multiple bootdevice, eMMC or UFS
,这毕竟是安卓的内核,启动设备仅支持emmc和ufs的识别,在linux里面还加了nvme、ssd吧。请注意这个ufs是universal flash storage的缩写,并不是unix file system的缩写,对于软件工程师来说,这可能有点迷惑。bootdevice驱动通过一些方法得到启动盘的的属性,再通过proc导出给上层使用。 bootdevice记录的属性有:盘的类型、cid属性、大小(单位是字节)、盘名、slc和mlc的平均寿命参数、制造商id等。下面是代码定义
struct __bootdevice { enum bootdevice_type type; const struct device *dev; sector_t size; u32 cid[4]; char product_name[MAX_NAME_LEN + 1]; u8 pre_eol_info; u8 life_time_est_typ_a; u8 life_time_est_typ_b; unsigned int manfid;};
type是从设备树中解析出来的,设备数需要提前填充好,解析代码如下:
boot_np = of_find_compatible_node(NULL, NULL, "hisilicon,bootdevice"); if (!boot_np) { pr_err("can't find bootdevice dts node\n"); err = -ENODEV; goto sys_unmap; } if (of_find_property(boot_np, "boot-from-emmc", NULL)) type = BOOT_DEVICE_EMMC else if (of_find_property(boot_np, "boot-from-ufs", NULL)) type = BOOT_DEVICE_UFS; else { type = ((readl(sysctrl + SCINNERSTAT) & EMMC_UFS_SEL) == BOOT_EMMC) ? BOOT_DEVICE_EMMC : BOOT_DEVICE_UFS; }
在设备数中找到hisilicon,bootdevice
字段,记录在boot_np参数中,如果没有找到,报错结束,proc下就看不到内容。如果找到节点之后,根据设备数中填充的boot from内容,得出type的值。emmc和ufs的定义如下:
enum bootdevice_type { BOOT_DEVICE_EMMC = 0, BOOT_DEVICE_UFS = 1 };
size属性获取需要分emmc和ufs两种,ufs调用ufs驱动获取到ufs 描述符计算出来的。
err = ufshcd_read_geometry_desc(hba, desc_buf, QUERY_DESC_GEOMETRY_MAZ_SIZE); if (err) { dev_err(hba->dev, "%s: Failed getting geometry info\n", __func__); goto out; } total_raw_device_capacity = (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 0] << 56 | (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 1] << 48 | (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 2] << 40 | (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 3] << 32 | (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 4] << 24 | (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 5] << 16 | (u64)desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 6] << 8 | desc_buf[GEOMETRY_DESC_TOTAL_DEVICE_CAPACITY + 7] << 0;...... set_bootdevice_size(total_raw_device_capacity);
mmc的size是调mmc接口计算的,计算大小是每个存储器驱动都会提供的基本功能了。
cid是指的是mmc的cid(Card Identification Register)寄存器,这个 CID 寄存器有 16 字节长,如下表所示,它包含了本卡的特别识别码(ID 号)。 这些信息是在卡的生产期间被编程(烧录),主控制器不能修改它们的内容。mmc直接发指令就能读到cid。
/* * Fetch CID from card. */ if (mmc_host_is_spi(host)) err = mmc_send_cid(host, cid); else err = mmc_all_send_cid(host, cid); if (err) goto err;......if (get_bootdevice_type() == BOOT_DEVICE_EMMC) { set_bootdevice_cid(cid); set_bootdevice_product_name(card->cid.prod_name, sizeof(card->cid.prod_name)); set_bootdevice_manfid(card->cid.manfid); }
对应ufs卡来说,并没有cid寄存器,所以用serial number+制造商id+制造时间计算了一个。
/** * UNIQUE NUMBER definition */struct ufs_unique_number { uint8_t serial_number[12]; uint16_t manufacturer_date; uint16_t manufacturer_id;}; ...... memcpy(hba->unique_number.serial_number, snum_buf, SERIAL_NUM_SIZE); ...... if (get_bootdevice_type() == BOOT_DEVICE_UFS) { u32 cid[4]; int i; memcpy(cid, (u32 *)&hba->unique_number, sizeof(cid)); for (i = 0; i < 3; i++) cid[i] = be32_to_cpu(cid[i]); cid[3] = (((cid[3]) & 0xffff) << 16) | (((cid[3]) >> 16) & 0xffff); set_bootdevice_cid((u32 *)cid); set_bootdevice_product_name(card_data->model, MAX_MODEL_LEN); set_bootdevice_manfid(hba->manufacturer_id); }
ufs先调用接口,读出serial number、manufacturer id和manufacturer date,最后把内容一次拷贝到cid存储空间中去。
剩下的三个参数,都是用来描述盘的使用寿命,具体的我也没看懂。这是官方提供的解释:PRE_EOL_INFO: Indicates the device lifetime reflected by average re-served blocksDEVICE_LIFE_TIME_EST_TYP_A: Indicates the estimation of the device lifetime that isreflected by the averaged wear out of SLC areas rela-tive to its maximum estimated device lifetime. DEVICE_LIFE_TIME_EST_TYP_B: Indicates the estimation of the device lifetime which isreflected by the averaged wear out of MLC areas rela-tive to its maximum estimated device lifetime
emmc通过mmc命令读取,ufs封装在描述符里,调用ufs读描述符接口读。最后,在proc下提供接口,填充proc ops里read函数即可。
if (!proc_mkdir("bootdevice", NULL)) { pr_err("make proc dir bootdevice failed\n"); return -EFAULT; } proc_create("bootdevice/type", 0, NULL, &type_proc_fops); proc_create("bootdevice/name", 0, NULL, &name_proc_fops); proc_create("bootdevice/size", 0, NULL, &size_proc_fops); proc_create("bootdevice/cid", 0, NULL, &cid_proc_fops); proc_create("bootdevice/product_name", 0, NULL, &product_name_proc_fops); proc_create("bootdevice/pre_eol_info", 0, NULL, &pre_eol_info_proc_fops); proc_create("bootdevice/life_time_est_typ_a", 0, NULL, &life_time_est_typ_a_proc_fops); proc_create("bootdevice/life_time_est_typ_b", 0, NULL, &life_time_est_typ_b_proc_fops); proc_create("bootdevice/manfid", 0, NULL, &manfid_proc_fops); return 0;
转载地址:http://hsbsi.baihongyu.com/