4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongGi Jang <dg0402.jang@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
27 #include <sys/ioctl.h>
28 #include <sys/mount.h>
30 #include <sys/types.h>
38 #include "device_engine.h"
40 #define MMC_DEV "/dev/mmcblk"
41 #define MMC_MOUNT_PATH "/opt/storage/sdcard"
43 static int exec_process(char *name, const char *arg1)
58 for (i = 0; i < _NSIG; i++)
60 execl(name, name, arg1, NULL);
61 ERR("execlp() error : %s\n", strerror(errno));
71 static int get_mmcblk_num()
76 /* mmcblk0 and mmcblk1 is reserved for movinand */
77 for (i = 9; i > 0; i--) {
78 snprintf(buf, 255, "%s%d", MMC_DEV, i);
79 if (access(buf, R_OK) == 0)
82 ERR("Failed to find mmc block number\n");
86 int format_mmc(int blknum)
89 struct stat parent_stat, mount_stat;
91 /* check MMC_MOUNT_FULL_PATH length */
92 char parent_path[PATH_MAX], mount_path[PATH_MAX];
93 int ret_f = 0, ret_s = 0;
95 char dev_mmcblk[NAME_MAX];
99 vconf_set_int(VCONFKEY_SYSMAN_MMC_FORMAT,
100 VCONFKEY_SYSMAN_MMC_FORMAT_FAILED);
103 snprintf(dev_mmcblk, NAME_MAX, "%s%d", MMC_DEV, blknum);
105 snprintf(parent_path, sizeof(parent_path), "%s/%s", MMC_MOUNT_PATH, "..");
106 snprintf(mount_path, sizeof(mount_path), "%s", MMC_MOUNT_PATH);
108 if (stat(mount_path, &mount_stat) != 0) {
109 ERR("get stat error : %s\n", mount_path);
111 } else if (stat(parent_path, &parent_stat) != 0) {
112 ERR("get stat error : %s\n", parent_path);
116 else if (mount_stat.st_dev == parent_stat.st_dev) {
117 ERR("Does not mounted, same st_dev\n");
120 ERR("MMC mounted, diff st_dev\n");
124 if (umount2(MMC_MOUNT_PATH, MNT_FORCE) == -1) {
125 ERR("devman mmc_format umount fail\n");
126 if (umount2(MMC_MOUNT_PATH, MNT_FORCE) == -1) {
127 vconf_set_int(VCONFKEY_SYSMAN_MMC_FORMAT,
128 VCONFKEY_SYSMAN_MMC_FORMAT_FAILED);
129 ERR("devman mmc_format umount retry fail\n");
135 fd = open(dev_mmcblk, O_RDWR);
137 ERR("%s open fail", dev_mmcblk);
141 memset(buf, 0, sizeof(buf));
142 if (write(fd, buf, sizeof(buf)) < 0) {
143 ERR("%s write fail to erase partition\n", dev_mmcblk);
150 if (ioctl(fd, BLKRRPART, NULL) < 0) {
151 ERR("%s ioctl fail\n", dev_mmcblk);
159 exec_process("/sbin/mkfs.vfat", dev_mmcblk);
160 if (mkfs_pid == -1) {
161 ERR("devman mmc_format mkfs.vfat fail\n");
168 snprintf(buf, sizeof(buf), "%s%d", "/proc/", mkfs_pid);
171 if (access(buf, R_OK) != 0)
176 if (ret_f == 0 && ret_s == 0) {
178 (dev_mmcblk, MMC_MOUNT_PATH, "vfat", 0,
179 "uid=0,gid=0,dmask=0000,fmask=0111,iocharset=iso8859-1,utf8,shortname=mixed")
181 ERR("Failed to mount mmc card, %s,%s\n", dev_mmcblk,
183 vconf_set_int(VCONFKEY_SYSMAN_MMC_FORMAT,
184 VCONFKEY_SYSMAN_MMC_FORMAT_FAILED);
185 heynoti_publish("mmcblk_remove");
188 if (heynoti_publish("mmcblk_add") == -1
190 ERR("notification failed for new MMC\n");
192 vconf_set_int(VCONFKEY_SYSMAN_MMC_FORMAT,
193 VCONFKEY_SYSMAN_MMC_FORMAT_FAILED);
194 ERR("result format process fail\n");
195 heynoti_publish("mmcblk_remove");
199 vconf_set_int(VCONFKEY_SYSMAN_MMC_FORMAT,
200 VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED);
204 static int set_mmc_int(int prop, int val)
208 case MMC_PROP_FORMAT:
209 ret = format_mmc(get_mmcblk_num());
217 static struct device mmc = {
219 .set_int = set_mmc_int,
220 .devtype = DEVTYPE_MMC,
223 static void __attribute__ ((constructor)) module_init()