Android Nand Flash 分区

本文详细解析了Android手机的分区说明,包括分区的名称、作用以及各分区的详细内容,如MISC、recovery、boot、system、userdata等分区的用途和结构,同时介绍了文件系统分区的挂载方式和权限设置,为深入理解Android手机的内部结构提供了全面指南。
摘要由CSDN通过智能技术生成

转自:http://blog.csdn.net/qq258711519/article/details/8667325

一般的嵌入式Linux开发人员是要清晰区分不同器材的作用和名称的。比如系统,内核所有保存在Nand Flash之上,断电后仍然存在,而运行后程序是装入SDRAM或Mobile DDR之类的内存设备运行。 

       但一般用户不大明白这几个芯片的区别,一般是就用ROM来指Nand Flash,RAM来指SDRAM之类设备。虽然在嵌入式编程,是专门有称作ROM的器材,比如EEPROM。这里就是大家约定俗说法吧。因此这里的Android ROM实际就是指烧录Nand Flash的各种二进制文件。
       另外,卖数码产品的喜欢把Nand Flash称为内存,SD卡称为外存,所以交流也只好这样了。
  
 
从分区表看,
 <<Android Partitions Explained: boot, system, recovery, data, cache & misc>>
   http://www.addictivetips.com/mobile/android-partitions-explained-boot-system-recovery-data-cache-misc/
 <<HOWTO: Unpack, Edit, and Re-Pack Boot Images>>
   http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images

 

一、手机的分区说明


我的HTC G8信息是 ,注意这里是Android的LINUX内核能看到分区,并不表示Flash上的所有分区,我的理解Hboot和radio分区在toolbox就看不到。
    

<p><span class="pln">cat </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln">mtd</span>
<span class="pln">dev</span><span class="pun">:</span><span class="pln"> size erasesize name</span>
<span class="pln">mtd0</span><span class="pun">:</span><span class="pln"> </span><span class="lit">000a0000</span><span class="pln"> </span><span class="lit">00020000</span><span class="pln">   </span><span class="str">"misc"</span>
<span class="pln">mtd1</span><span class="pun">:</span><span class="pln"> </span><span class="lit">00420000</span><span class="pln"> </span><span class="lit">00020000</span><span class="pln">   </span><span class="str">"recovery"</span>
<span class="pln">mtd2</span><span class="pun">:</span><span class="pln"> </span><span class="lit">002c0000</span><span class="pln"> </span><span class="lit">00020000</span><span class="pln">   </span><span class="str">"boot"</span>
<span class="pln">mtd3</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0fa00000</span><span class="pln"> </span><span class="lit">00020000</span><span class="pln">   </span><span class="str">"system"</span>
<span class="pln">mtd4</span><span class="pun">:</span><span class="pln"> </span><span class="lit">02800000</span><span class="pln"> </span><span class="lit">00020000</span><span class="pln">   </span><span class="str">"cache"</span>
<span class="pln">mtd5</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0af20000</span><span class="pln"> </span><span class="lit">00020000</span><span class="pln">   </span><span class="str">"userdata"</span>
</p>
 

MISC分区


       其中misc分区信息第一篇文章有解释:保存设备配置信息:CID (Carrier or Region ID),USB和其它硬件设备配置信息,大约20K的样子。引自  http://blog.chinaunix.net/space.php?uid=20543672&do=blog&id=94411  可能结构是

<p><span class="lit">0x00</span><span class="pln">  CID</span>
<span class="lit">0x10</span><span class="pln">  enter_bootloader</span>
<span class="lit">0x20</span><span class="pln">  cold boot   </span><span class="pun">(</span><span class="typ">DeviceWarmBoot</span><span class="pun">)</span>
<span class="lit">0x30</span><span class="pln">  goupdateloader</span>
<span class="lit">0x40</span><span class="pln">  (NBH</span><span class="pun">)</span>
<span class="lit">0x50</span><span class="pln">  CE </span><span class="typ">Serial</span><span class="pln"> </span><span class="typ">InUse</span><span class="pln">\0</span>
<span class="lit">0x60</span><span class="pln">  </span><span class="typ">Debug</span><span class="pln"> </span><span class="typ">Cable</span><span class="pln"> </span><span class="typ">Ena</span><span class="pln">\0</span>
<span class="lit">0x70</span><span class="pln">  CE USB   </span><span class="typ">InUse</span><span class="pln">\0</span>
<span class="lit">0x80</span><span class="pln">  (IMG</span><span class="pun">)</span>
<span class="lit">0x90</span><span class="pln">  </span><span class="typ">ClearAutoImage</span><span class="pln"> \0</span>
<span class="lit">0xa0</span><span class="pln">  </span><span class="pun">*.**.***.*</span><span class="pln">\0</span><span class="pun">(</span><span class="typ">HBoot</span><span class="pln"> version</span><span class="pun">)</span></p>
 

recovery分区 


       recovery 分区即恢复分区,在正常分区被破坏后,仍可以进入这一分区进行备份和恢复.我的理解是这个分区保存一个简单的OS或底层软件,在Android的内核被破坏后可以用bootloader从这个分区引导进行操作。

boot 分区


        一般的嵌入式Linux的设备中.bootloader,内核,根文件系统被分为三个不同分区。在Android做得比较复杂,从这个手机分区和来看,这里boot分区是把内核和ramdisk file的根文件系统打包在一起了,是编译生成boot.img来烧录的。它有如下格式。   

 boot header 1 page
 kernel  n pages
 ramdisk m pages
 second stage o pages
n = (kernel_size + page_size - 1) / page_sizem = (ramdisk_size + page_size - 1) / page_sizeo = (second_size + page_size - 1) / page_size0. all entities are page_size aligned in flash1. kernel and ramdisk are required (size != 0)2. second is optional (second_size == 0 -> no second)这里表示boot分区有四个部分,其中前三部分是必须的,而第四部分 second stage 可选。 每部分的大小是一个flash page的倍数,不足一页的需要加入空格字符补齐成一页。其中bootl header是一些特定的数据结构。每种手机有细微的差别。但是它是有一个统一个数据结构参见 bootloader/legacy/include/boot/bootimg.h 中 boot_img_hdr

<p><span class="kwd">typedef</span><span class="pln"> </span><span class="kwd">struct</span><span class="pln"> boot_img_hdr boot_img_hdr</span><span class="pun">;</span>
<span class="com">#define</span><span class="pln"> BOOT_MAGIC   </span><span class="str">"ANDROID!"</span>
<span class="com">#define</span><span class="pln"> BOOT_MAGIC_SIZE </span><span class="lit">8</span>
<span class="com">#define</span><span class="pln"> BOOT_NAME_SIZE </span><span class="lit">16</span>
<span class="com">#define</span><span class="pln">   BOOT_ARGS_SIZE </span><span class="lit">512</span>
<span class="kwd">struct</span><span class="pln">   boot_img_hdr</span>
<span class="pun">{</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">char</span><span class="pln">   magic</span><span class="pun">[</span><span class="pln">BOOT_MAGIC_SIZE</span><span class="pun">];</span><span class="pln">            </span><span class="com">/*幻数,一般固定为   ANDROID! */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> kernel_size</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* size in bytes */</span><span class="pln">          </span><span class="com">/*内核长度                  */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> kernel_addr</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* physical   load addr */</span><span class="pln">   </span><span class="com">/*内核装入地址               */</span><span class="pln"> </span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> ramdisk_size</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* size in bytes */</span><span class="pln">         </span><span class="com">/*ramdisk 长度              */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> ramdisk_addr</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* physical load addr   */</span><span class="pln">  </span><span class="com">/* ramdisk 装入地址          */</span>
<span class="pln"> </span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> second_size</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* size in   bytes */</span><span class="pln">        </span><span class="com">/* second stage 长度        */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> second_addr</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* physical load addr   */</span><span class="pln">   </span><span class="com">/* second staget 装入地址  */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln">   tags_addr</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* physical addr for kernel tags */</span><span class="pln"> </span><span class="com">/*内核tags 即内核参数 物理地址 ?   */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> page_size</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* flash page size we assume   */</span><span class="pln">   </span><span class="com">/* flash页尺寸,取决于flash型号 */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln">   unused</span><span class="pun">[</span><span class="lit">2</span><span class="pun">];</span><span class="pln"> </span><span class="com">/* future expansion: should be 0 */</span><span class="pln"> </span><span class="com">/* 保留未用字段   */</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">char</span><span class="pln"> name</span><span class="pun">[</span><span class="pln">BOOT_NAME_SIZE</span><span class="pun">];</span><span class="pln"> </span><span class="com">/* asciiz   product name */</span><span class="pln">  </span><span class="com">/*产品名称   */</span><span class="pln">    </span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">char</span><span class="pln">   cmdline</span><span class="pun">[</span><span class="pln">BOOT_ARGS_SIZE</span><span class="pun">];</span><span class="pln">                     </span><span class="com">/* Linux 内核引导参数,*/</span>
<span class="pln">    </span><span class="kwd">unsigned</span><span class="pln"> id</span><span class="pun">[</span><span class="lit">8</span><span class="pun">];</span><span class="pln"> </span><span class="com">/*   timestamp / checksum / sha1 / etc */</span><span class="pln">   </span><span class="com">/*检验值   */</span>
<span class="pun">};</span>
</p>
 这里的内核tags,应该就是指内核命令行参数,在头文件里有如下注释注明了,bootloader在引导LINUX 内核时,将会把寄存器r2保存tags addr,而在ARM-LINUX定义里 r1是机器码,而r2就是引志命令行参数的偏移量,
** 4. prepare tags at tag_addr. kernel_args[] is ** appended to the kernel commandline in the tags. ** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
而kernel 和ramdisk则是LINUX标准的zImage和zip格式,这里略掉其说明
文件系统分区. Linux必须有一个根文件系统分区,可以为多种格式,这里用的是可读的ramdisk 格式,它启动分隔一部分内存,挂载到/目录下。 然后再用分三个不同权限分区来装载不同子目录.这里二个子目录是 /system ,/userdata 并且内容是完全只读的它将必须用root用户 这种设计结构的出发点是这样考虑,内核和根文件系统的由手机制造商控制,不让用户修改,而且system的分区保存重要的系统命令和框架程序。由官方来升级,对于用户是只读的。而且userdata目录才是用户自行管理的,比如下载的应用。 而启动时最重要的root用户并未对用户公开,应用程序都是用普通用户的如 app_xx这样的帐号来运行。这样可以有效保护/system的程序。 但是第三方的自制rom往往要修改/system的内容,因此刷机时要通过破解方法来取得root用户权限。
system 分区

这里是挂载到/system目录下的分区,是一个yaffs2的文件系统,用普通的adb 命令是无法操作这个目录的。这里有 /system/bin 和 /system/sbin 保存很多系统命令。它是由编译出来的system.img来烧入。
userdata 分区 

它也是一个yaffs2文件系统,它将挂载到 /data 目录下, 它是由编译出来的userdata.img来烧入。cache 分区 它也是一个yaffs2文件系统,它将挂载到 /cache 目录下,看一般解释,这里主要用升级的缓存,内容由运行而定.
<p><span class="pln">cat </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln">mounts</span>
<span class="pln">rootfs </span><span class="pun">/</span><span class="pln"> rootfs ro</span><span class="pun">,</span><span class="pln">relatime </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="com">#根文件系统的格式,只读</span>
<span class="pln">tmpfs </span><span class="pun">/</span><span class="pln">dev tmpfs rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">mode</span><span class="pun">=</span><span class="lit">755</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
<span class="pln">devpts </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">pts devpts rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">mode</span><span class="pun">=</span><span class="lit">600</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
<span class="pln">proc </span><span class="pun">/</span><span class="pln">proc proc rw</span><span class="pun">,</span><span class="pln">relatime </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
<span class="pln">sysfs </span><span class="pun">/</span><span class="pln">sys sysfs rw</span><span class="pun">,</span><span class="pln">relatime </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
<span class="pln">none </span><span class="pun">/</span><span class="pln">acct cgroup rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">cpuacct </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
<span class="pln">tmpfs </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">asec tmpfs rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">mode</span><span class="pun">=</span><span class="lit">755</span><span class="pun">,</span><span class="pln">gid</span><span class="pun">=</span><span class="lit">1000</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span>
<span class="pln">none </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">cpuctl cgroup rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">cpu </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">block</span><span class="pun">/</span><span class="pln">mtdblock3 </span><span class="pun">/</span><span class="pln">system yaffs2 ro</span><span class="pun">,</span><span class="pln">relatime </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="com">#system分区,只读</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">block</span><span class="pun">/</span><span class="pln">mtdblock5 </span><span class="pun">/</span><span class="pln">data yaffs2 rw</span><span class="pun">,</span><span class="pln">nosuid</span><span class="pun">,</span><span class="pln">nodev</span><span class="pun">,</span><span class="pln">relatime </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="com">#data分区,可读写</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">block</span><span class="pun">/</span><span class="pln">mtdblock4 </span><span class="pun">/</span><span class="pln">cache yaffs2 rw</span><span class="pun">,</span><span class="pln">nosuid</span><span class="pun">,</span><span class="pln">nodev</span><span class="pun">,</span><span class="pln">relatime </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="com">#cache分区,可读写</span>
</p>
 
其它隐藏分区:
 

  HBOOT 这里没有看,保存的bootloader HBOOT,从源码看不是u-boot的变种。
 
  Radio分区  保存是基带芯片的固件代码,Linux不认识其格式,在手机启动时装入特定内存中用于驱动芯片。所有与电信网络交互就是靠它了,一般往往用专用开发环境来开发。
  splash分区  这里是启动画面。
  SD卡分区  一般默认的是挂载在/sdcard目录,从我的机器看,好象没有挂上。
  
SD卡扩展分区
 

   它的目录名是 /sd-ext ,它不是一个标准的Android分区,是运行APP2D软件扩展出来分区。目的是为了多扩展一个安装程序空间,这个对于Flash空间(或者说ROM空间)不够,又喜欢安装软件的人是有用应用。
二.各分区详细分析
  各个分区的内容,可以用cat命令直接导出,用一般的二进制的软件来分析,我一般用WinHex,并且自己写了几个模板。
  导出分区内容,如果用adb 导出,必须有root权限,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值