Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128216
  • 博文数量: 16
  • 博客积分: 355
  • 博客等级: 一等列兵
  • 技术积分: 237
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-01 22:01
文章分类

全部博文(16)

文章存档

2012年(16)

我的朋友

分类: 嵌入式

2012-08-08 11:47:04


  1. 0.kernel添加ubifs的支持
  2. Device Drivers --->
  3.     Memory Technology Device(MTD) support --->
  4.       UBI - Unsorted block images --->
  5.         <*> Enable UBI - Unsorted block images

  6. File systems --->
  7.     Miscellaneous filesystems --->
  8.         <*> UBIFS file system support
  9.      [*] Advanced compression options
  10.         [*] LZO compression support
  11.         [*] ZLIB compression support
  12. 1. andriod/system/core/rootdir/init.rc
  13.     mount yaffs2 mtd@userdata /data nosuid nodev
  14. 改为:
  15.     mount ubifs ubi@userdata /data nosuid nodev
  16. 2. andriod/system/core/init/builtins.c

  17. } else if (!strncmp(source, "loop@", 5)) {
  18. 之前加上
  19. + }else if (!strncmp(source, "ubi@", 4)) {
  20. + n = ubi_attach_mtd(source + 4);
  21. + if (n < 0) {
  22. + return -1;
  23. + }
  24. + sprintf(tmp, "/dev/ubi%d_0", n);
  25. + if (wait)
  26. + wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
  27. + if (mount(tmp, target, system, flags, options) < 0) {
  28. + ubi_detach_dev(n);
  29. + return -1;
  30. + }
  31. + return 0;
  32. + }else if (!strncmp(source, "loop@", 5)) {

  33. 3. andriod/system/core/init/init.c

  34. 在static int property_triggers_enabled = 0;之后加上
  35. +static unsigned ubifs_enabled = 1;
  36. static int set_init_properties_action(int nargs, char **args)
  37. {
  38.     property_set("ro.revision", tmp);
  39. + property_set("ro.ubifs",ubifs_enabled ? "1" : "0");
  40.     return 0;
  41. }

  42. int main(int argc, char **argv)
  43. {
  44.    action_for_each_trigger("post-fs", action_add_queue_tail);
  45. + action_for_each_trigger("ubi-fs", action_add_queue_tail);
  46. }

  47. 4. andriod/system/core/init/util.h
  48. void get_hardware_name(char *hardware, unsigned int *revision);
  49. +int ubi_attach_mtd(const char *name);
  50. +int ubi_detach_dev(int dev);

  51. 5. andriod/system/core/init/util.c
  52. #include <sys/un.h>
  53. +#include <sys/ioctl.h>

  54. #include "util.h"
  55. +#include "ubi-user.h"

  56. +#define UBI_CTRL_DEV "/dev/ubi_ctrl"
  57. +#define UBI_SYS_PATH "/sys/class/ubi"

  58. 在最后添加下面三个函数
  59. static int ubi_dev_read_int(int dev, const char *file, int def)
  60. {
  61.     int fd, val = def;
  62.     char path[128], buf[64];
  63.     sprintf(path, UBI_SYS_PATH "/ubi%d/%s", dev, file);
  64.     wait_for_file(path, 5);
  65.     fd = open(path, O_RDONLY);
  66.     if (fd == -1) {
  67.         return val;
  68.     }
  69.     if (read(fd, buf, 64) > 0) {
  70.         val = atoi(buf);
  71.     }
  72.     close(fd);
  73.     return val;
  74. }
  75. int ubi_attach_mtd(const char *name)
  76. {
  77.     int ret;
  78.     int mtd_num, ubi_num;
  79.     int ubi_ctrl, ubi_dev;
  80.     int vols, avail_lebs, leb_size;
  81.     char path[128];
  82.     struct ubi_attach_req attach_req;
  83.     struct ubi_mkvol_req mkvol_req;

  84.     mtd_num = mtd_name_to_number(name);
  85.     if (mtd_num == -1) {
  86.         return -1;
  87.     }
  88.     ubi_ctrl = open(UBI_CTRL_DEV, O_RDONLY);
  89.     if (ubi_ctrl == -1) {
  90.         return -1;
  91.     }
  92.     memset(&attach_req, 0, sizeof(struct ubi_attach_req));
  93.     attach_req.ubi_num = UBI_DEV_NUM_AUTO;
  94.     attach_req.mtd_num = mtd_num;
  95.     ret = ioctl(ubi_ctrl, UBI_IOCATT, &attach_req);
  96.     if (ret == -1) {
  97.         close(ubi_ctrl);
  98.         return -1;
  99.     }
  100.     ubi_num = attach_req.ubi_num;

  101.     vols = ubi_dev_read_int(ubi_num, "volumes_count", -1);
  102.     if (vols == 0) {
  103.         sprintf(path, "/dev/ubi%d", ubi_num);
  104.         ubi_dev = open(path, O_RDONLY);
  105.         if (ubi_dev == -1) {
  106.             close(ubi_ctrl);
  107.             return ubi_num;
  108.         }

  109.         avail_lebs = ubi_dev_read_int(ubi_num, "avail_eraseblocks", 0);
  110.         leb_size = ubi_dev_read_int(ubi_num, "eraseblock_size", 0);
  111.         memset(&mkvol_req, 0, sizeof(struct ubi_mkvol_req));
  112.         mkvol_req.vol_id = UBI_VOL_NUM_AUTO;
  113.         mkvol_req.alignment = 1;
  114.         mkvol_req.bytes = (long long)avail_lebs * leb_size;
  115.         mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
  116.         ret = snprintf(mkvol_req.name, UBI_MAX_VOLUME_NAME + 1, "%s", name);
  117.         mkvol_req.name_len = ret;
  118.         ioctl(ubi_dev, UBI_IOCMKVOL, &mkvol_req);
  119.         close(ubi_dev);
  120.     }
  121.     close(ubi_ctrl);
  122.     return ubi_num;
  123. }

  124. int ubi_detach_dev(int dev)
  125. {
  126.     int ret, ubi_ctrl;
  127.     ubi_ctrl = open(UBI_CTRL_DEV, O_RDONLY);
  128.     if (ubi_ctrl == -1) {
  129.         return -1;
  130.     }
  131.     ret = ioctl(ubi_ctrl, UBI_IOCDET, &dev);
  132.     close(ubi_ctrl);
  133.     return ret;
  134. }

  135. 6.增加文件
  136. andriod/system/core/init/ubi-user.h
  137. /*
  138.  * Copyright (c) International Business Machines Corp., 2006
  139.  *
  140.  * This program is free software; you can redistribute it and/or modify
  141.  * it under the terms of the GNU General Public License as published by
  142.  * the Free Software Foundation; either version 2 of the License, or
  143.  * (at your option) any later version.
  144.  *
  145.  * This program is distributed in the hope that it will be useful,
  146.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  147.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  148.  * the GNU General Public License for more details.
  149.  *
  150.  * You should have received a copy of the GNU General Public License
  151.  * along with this program; if not, write to the Free Software
  152.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  153.  *
  154.  * Author: Artem Bityutskiy (Битюцкий Артём)
  155.  */

  156. #ifndef __UBI_USER_H__
  157. #define __UBI_USER_H__

  158. /*
  159.  * UBI device creation (the same as MTD device attachment)
  160.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  161.  *
  162.  * MTD devices may be attached using %UBI_IOCATT ioctl command of the UBI
  163.  * control device. The caller has to properly fill and pass
  164.  * &struct ubi_attach_req object - UBI will attach the MTD device specified in
  165.  * the request and return the newly created UBI device number as the ioctl
  166.  * return value.
  167.  *
  168.  * UBI device deletion (the same as MTD device detachment)
  169.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170.  *
  171.  * An UBI device maybe deleted with %UBI_IOCDET ioctl command of the UBI
  172.  * control device.
  173.  *
  174.  * UBI volume creation
  175.  * ~~~~~~~~~~~~~~~~~~~
  176.  *
  177.  * UBI volumes are created via the %UBI_IOCMKVOL IOCTL command of UBI character
  178.  * device. A &struct ubi_mkvol_req object has to be properly filled and a
  179.  * pointer to it has to be passed to the IOCTL.
  180.  *
  181.  * UBI volume deletion
  182.  * ~~~~~~~~~~~~~~~~~~~
  183.  *
  184.  * To delete a volume, the %UBI_IOCRMVOL IOCTL command of the UBI character
  185.  * device should be used. A pointer to the 32-bit volume ID hast to be passed
  186.  * to the IOCTL.
  187.  *
  188.  * UBI volume re-size
  189.  * ~~~~~~~~~~~~~~~~~~
  190.  *
  191.  * To re-size a volume, the %UBI_IOCRSVOL IOCTL command of the UBI character
  192.  * device should be used. A &struct ubi_rsvol_req object has to be properly
  193.  * filled and a pointer to it has to be passed to the IOCTL.
  194.  *
  195.  * UBI volume update
  196.  * ~~~~~~~~~~~~~~~~~
  197.  *
  198.  * Volume update should be done via the %UBI_IOCVOLUP IOCTL command of the
  199.  * corresponding UBI volume character device. A pointer to a 64-bit update
  200.  * size should be passed to the IOCTL. After this, UBI expects user to write
  201.  * this number of bytes to the volume character device. The update is finished
  202.  * when the claimed number of bytes is passed. So, the volume update sequence
  203.  * is something like:
  204.  *
  205.  * fd = open("/dev/my_volume");
  206.  * ioctl(fd, UBI_IOCVOLUP, &image_size);
  207.  * write(fd, buf, image_size);
  208.  * close(fd);
  209.  *
  210.  * Atomic eraseblock change
  211.  * ~~~~~~~~~~~~~~~~~~~~~~~~
  212.  *
  213.  * Atomic eraseblock change operation is done via the %UBI_IOCEBCH IOCTL
  214.  * command of the corresponding UBI volume character device. A pointer to
  215.  * &struct ubi_leb_change_req has to be passed to the IOCTL. Then the user is
  216.  * expected to write the requested amount of bytes. This is similar to the
  217.  * "volume update" IOCTL.
  218.  */

  219. /*
  220.  * When a new UBI volume or UBI device is created, users may either specify the
  221.  * volume/device number they want to create or to let UBI automatically assign
  222.  * the number using these constants.
  223.  */
  224. #define UBI_VOL_NUM_AUTO (-1)
  225. #define UBI_DEV_NUM_AUTO (-1)

  226. /* Maximum volume name length */
  227. #define UBI_MAX_VOLUME_NAME 127

  228. /* IOCTL commands of UBI character devices */

  229. #define UBI_IOC_MAGIC 'o'

  230. /* Create an UBI volume */
  231. #define UBI_IOCMKVOL _IOW(UBI_IOC_MAGIC, 0, struct ubi_mkvol_req)
  232. /* Remove an UBI volume */
  233. #define UBI_IOCRMVOL _IOW(UBI_IOC_MAGIC, 1, int32_t)
  234. /* Re-size an UBI volume */
  235. #define UBI_IOCRSVOL _IOW(UBI_IOC_MAGIC, 2, struct ubi_rsvol_req)

  236. /* IOCTL commands of the UBI control character device */

  237. #define UBI_CTRL_IOC_MAGIC 'o'

  238. /* Attach an MTD device */
  239. #define UBI_IOCATT _IOW(UBI_CTRL_IOC_MAGIC, 64, struct ubi_attach_req)
  240. /* Detach an MTD device */
  241. #define UBI_IOCDET _IOW(UBI_CTRL_IOC_MAGIC, 65, int32_t)

  242. /* IOCTL commands of UBI volume character devices */

  243. #define UBI_VOL_IOC_MAGIC 'O'

  244. /* Start UBI volume update */
  245. #define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t)
  246. /* An eraseblock erasure command, used for debugging, disabled by default */
  247. #define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, int32_t)
  248. /* An atomic eraseblock change command */
  249. #define UBI_IOCEBCH _IOW(UBI_VOL_IOC_MAGIC, 2, int32_t)

  250. /* Maximum MTD device name length supported by UBI */
  251. #define MAX_UBI_MTD_NAME_LEN 127

  252. /*
  253.  * UBI data type hint constants.
  254.  *
  255.  * UBI_LONGTERM: long-term data
  256.  * UBI_SHORTTERM: short-term data
  257.  * UBI_UNKNOWN: data persistence is unknown
  258.  *
  259.  * These constants are used when data is written to UBI volumes in order to
  260.  * help the UBI wear-leveling unit to find more appropriate physical
  261.  * eraseblocks.
  262.  */
  263. enum {
  264.     UBI_LONGTERM = 1,
  265.     UBI_SHORTTERM = 2,
  266.     UBI_UNKNOWN = 3,
  267. };

  268. /*
  269.  * UBI volume type constants.
  270.  *
  271.  * @UBI_DYNAMIC_VOLUME: dynamic volume
  272.  * @UBI_STATIC_VOLUME: static volume
  273.  */
  274. enum {
  275.     UBI_DYNAMIC_VOLUME = 3,
  276.     UBI_STATIC_VOLUME = 4,
  277. };

  278. /**
  279.  * struct ubi_attach_req - attach MTD device request.
  280.  * @ubi_num: UBI device number to create
  281.  * @mtd_num: MTD device number to attach
  282.  * @vid_hdr_offset: VID header offset (use defaults if %0)
  283.  * @padding: reserved for future, not used, has to be zeroed
  284.  *
  285.  * This data structure is used to specify MTD device UBI has to attach and the
  286.  * parameters it has to use. The number which should be assigned to the new UBI
  287.  * device is passed in @ubi_num. UBI may automatically assign the number if
  288.  * @UBI_DEV_NUM_AUTO is passed. In this case, the device number is returned in
  289.  * @ubi_num.
  290.  *
  291.  * Most applications should pass %0 in @vid_hdr_offset to make UBI use default
  292.  * offset of the VID header within physical eraseblocks. The default offset is
  293.  * the next min. I/O unit after the EC header. For example, it will be offset
  294.  * 512 in case of a 512 bytes page NAND flash with no sub-page support. Or
  295.  * it will be 512 in case of a 2KiB page NAND flash with 4 512-byte sub-pages.
  296.  *
  297.  * But in rare cases, if this optimizes things, the VID header may be placed to
  298.  * a different offset. For example, the boot-loader might do things faster if the
  299.  * VID header sits at the end of the first 2KiB NAND page with 4 sub-pages. As
  300.  * the boot-loader would not normally need to read EC headers (unless it needs
  301.  * UBI in RW mode), it might be faster to calculate ECC. This is weird example,
  302.  * but it real-life example. So, in this example, @vid_hdr_offer would be
  303.  * 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes
  304.  * aligned, which is OK, as UBI is clever enough to realize this is 4th sub-page
  305.  * of the first page and add needed padding.
  306.  */
  307. struct ubi_attach_req {
  308.     int32_t ubi_num;
  309.     int32_t mtd_num;
  310.     int32_t vid_hdr_offset;
  311.     uint8_t padding[12];
  312. };

  313. /**
  314.  * struct ubi_mkvol_req - volume description data structure used in
  315.  * volume creation requests.
  316.  * @vol_id: volume number
  317.  * @alignment: volume alignment
  318.  * @bytes: volume size in bytes
  319.  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  320.  * @padding1: reserved for future, not used, has to be zeroed
  321.  * @name_len: volume name length
  322.  * @padding2: reserved for future, not used, has to be zeroed
  323.  * @name: volume name
  324.  *
  325.  * This structure is used by user-space programs when creating new volumes. The
  326.  * @used_bytes field is only necessary when creating static volumes.
  327.  *
  328.  * The @alignment field specifies the required alignment of the volume logical
  329.  * eraseblock. This means, that the size of logical eraseblocks will be aligned
  330.  * to this number, i.e.,
  331.  *    (UBI device logical eraseblock size) mod (@alignment) = 0.
  332.  *
  333.  * To put it differently, the logical eraseblock of this volume may be slightly
  334.  * shortened in order to make it properly aligned. The alignment has to be
  335.  * multiple of the flash minimal input/output unit, or %1 to utilize the entire
  336.  * available space of logical eraseblocks.
  337.  *
  338.  * The @alignment field may be useful, for example, when one wants to maintain
  339.  * a block device on top of an UBI volume. In this case, it is desirable to fit
  340.  * an integer number of blocks in logical eraseblocks of this UBI volume. With
  341.  * alignment it is possible to update this volume using plane UBI volume image
  342.  * BLOBs, without caring about how to properly align them.
  343.  */
  344. struct ubi_mkvol_req {
  345.     int32_t vol_id;
  346.     int32_t alignment;
  347.     int64_t bytes;
  348.     int8_t vol_type;
  349.     int8_t padding1;
  350.     int16_t name_len;
  351.     int8_t padding2[4];
  352.     char name[UBI_MAX_VOLUME_NAME + 1];
  353. } __attribute__ ((packed));

  354. /**
  355.  * struct ubi_rsvol_req - a data structure used in volume re-size requests.
  356.  * @vol_id: ID of the volume to re-size
  357.  * @bytes: new size of the volume in bytes
  358.  *
  359.  * Re-sizing is possible for both dynamic and static volumes. But while dynamic
  360.  * volumes may be re-sized arbitrarily, static volumes cannot be made to be
  361.  * smaller then the number of bytes they bear. To arbitrarily shrink a static
  362.  * volume, it must be wiped out first (by means of volume update operation with
  363.  * zero number of bytes).
  364.  */
  365. struct ubi_rsvol_req {
  366.     int64_t bytes;
  367.     int32_t vol_id;
  368. } __attribute__ ((packed));

  369. /**
  370.  * struct ubi_leb_change_req - a data structure used in atomic logical
  371.  * eraseblock change requests.
  372.  * @lnum: logical eraseblock number to change
  373.  * @bytes: how many bytes will be written to the logical eraseblock
  374.  * @dtype: data type (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
  375.  * @padding: reserved for future, not used, has to be zeroed
  376.  */
  377. struct ubi_leb_change_req {
  378.     int32_t lnum;
  379.     int32_t bytes;
  380.     uint8_t dtype;
  381.     uint8_t padding[7];
  382. } __attribute__ ((packed));

  383. #endif /* __UBI_USER_H__ */
阅读(5067) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~