partition_mt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /******************************************************************************
  2. * partition_mt.c - MT6516 NAND partition management Driver
  3. *
  4. * Copyright 2009-2010 MediaTek Co.,Ltd.
  5. *
  6. * DESCRIPTION:
  7. * This file provid the other drivers partition relative functions
  8. *
  9. * modification history
  10. * ----------------------------------------
  11. * v1.0, 28 Feb 2011, mtk80134 written
  12. * ----------------------------------------
  13. ******************************************************************************/
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/types.h>
  22. #include <linux/wait.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mtd/nand_ecc.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/time.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <asm/io.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/uaccess.h>
  35. /* #include <mach/mt_typedefs.h> */
  36. /* #include <mach/mt_clkmgr.h> */
  37. /* #include <mach/mtk_nand.h> */
  38. #include <nand_device_define.h>
  39. #include <mtk_nand.h>
  40. /* #include "board-custom.h" */
  41. /* #include <mach/pmt.h> */
  42. #include <pmt.h>
  43. /* #include "partition.h" */
  44. /* #include <mach/board.h> */
  45. #ifndef FALSE
  46. #define FALSE (0)
  47. #endif
  48. #ifndef TRUE
  49. #define TRUE (1)
  50. #endif
  51. #ifdef PMT
  52. unsigned long long partition_type_array[PART_MAX_COUNT];
  53. pt_resident new_part[PART_MAX_COUNT];
  54. pt_resident lastest_part[PART_MAX_COUNT];
  55. unsigned char part_name[PART_MAX_COUNT][MAX_PARTITION_NAME_LEN];
  56. struct mtd_partition g_pasStatic_Partition[PART_MAX_COUNT];
  57. int part_num = 1;
  58. /* struct excel_info PartInfo[PART_MAX_COUNT]; */
  59. #define MTD_SECFG_STR "seccnfg"
  60. #define MTD_BOOTIMG_STR "boot"
  61. #define MTD_ANDROID_STR "system"
  62. #define MTD_SECRO_STR "secstatic"
  63. #define MTD_USRDATA_STR "userdata"
  64. int block_size;
  65. int page_size;
  66. pt_info pi;
  67. u8 sig_buf[PT_SIG_SIZE];
  68. /* not support add new partition automatically. */
  69. /* extern struct mtd_partition g_pasStatic_Partition[]; */
  70. /* extern int part_num; */
  71. DM_PARTITION_INFO_PACKET pmtctl;
  72. struct mtd_partition g_exist_Partition[PART_MAX_COUNT];
  73. /* #define LPAGE 2048 */
  74. /* char page_buf[LPAGE+64]; */
  75. /* char page_readbuf[LPAGE]; */
  76. char *page_buf;
  77. char *page_readbuf;
  78. #define PMT_MAGIC 'p'
  79. #define PMT_READ _IOW(PMT_MAGIC, 1, int)
  80. #define PMT_WRITE _IOW(PMT_MAGIC, 2, int)
  81. #define PMT_VERSION _IOW(PMT_MAGIC, 3, int)
  82. #if 0
  83. struct pmt_config {
  84. int (*read_pmt)(char *buf);
  85. int (*write_pmt)(char *buf);
  86. };
  87. static struct pmt_config g_partition_fuc;
  88. #endif
  89. static bool init_pmt_done = FALSE;
  90. static int read_pmt(void __user *arg);
  91. void get_part_tab_from_complier(void)
  92. {
  93. /* int index=0; */
  94. pr_debug("get_pt_from_complier\n");
  95. memcpy(&g_exist_Partition, &g_pasStatic_Partition,
  96. sizeof(struct mtd_partition) * PART_MAX_COUNT);
  97. #if 0
  98. while (g_pasStatic_Partition[index].size != MTDPART_SIZ_FULL) { /* the last partition sizefull ==0 */
  99. memcpy(lastest_part[index].name, g_pasStatic_Partition[index].name,
  100. MAX_PARTITION_NAME_LEN);
  101. lastest_part[index].size = g_pasStatic_Partition[index].size;
  102. lastest_part[index].offset = g_pasStatic_Partition[index].offset;
  103. /* this flag in kernel should be fufilled even though in flash is 0. */
  104. lastest_part[index].mask_flags = g_pasStatic_Partition[index].mask_flags;
  105. pr_debug("get_ptr %s %llx\n", lastest_part[index].name,
  106. lastest_part[index].offset);
  107. index++;
  108. }
  109. /* get last partition info */
  110. memcpy(lastest_part[index].name, g_pasStatic_Partition[index].name, MAX_PARTITION_NAME_LEN);
  111. lastest_part[index].size = g_pasStatic_Partition[index].size;
  112. lastest_part[index].offset = g_pasStatic_Partition[index].offset;
  113. /* this flag in kernel should be fufilled even though in flash is 0. */
  114. lastest_part[index].mask_flags = g_pasStatic_Partition[index].mask_flags;
  115. pr_debug("get_ptr %s %llx\n", lastest_part[index].name,
  116. lastest_part[index].offset);
  117. #endif
  118. }
  119. u64 part_get_startaddress(u64 byte_address, u32 *idx)
  120. {
  121. int index = 0;
  122. if (TRUE == init_pmt_done) {
  123. while (index < part_num) {
  124. /* MSG(INIT, "g_exist_Partition[%d].offset %x\n",index, g_exist_Partition[index].offset); */
  125. if (g_exist_Partition[index].offset > byte_address) {
  126. /* MSG(INIT, "find g_exist_Partition[%d].offset %x\n",
  127. index-1, g_exist_Partition[index-1].offset); */
  128. *idx = index - 1;
  129. return g_exist_Partition[index - 1].offset;
  130. }
  131. index++;
  132. }
  133. }
  134. *idx = part_num - 1;
  135. return byte_address;
  136. }
  137. bool raw_partition(u32 index)
  138. {
  139. if (partition_type_array[index] == REGION_LOW_PAGE)
  140. return TRUE;
  141. return FALSE;
  142. }
  143. int find_empty_page_from_top(u64 start_addr, struct mtd_info *mtd)
  144. {
  145. int page_offset; /* ,i; */
  146. u64 current_add;
  147. #if defined(MTK_MLC_NAND_SUPPORT)
  148. int i;
  149. #endif
  150. struct mtd_oob_ops ops_pt;
  151. struct erase_info ei;
  152. ei.mtd = mtd;
  153. ei.len = mtd->erasesize;
  154. ei.time = 1000;
  155. ei.retries = 2;
  156. ei.callback = NULL;
  157. ops_pt.datbuf = (uint8_t *) page_buf;
  158. ops_pt.mode = MTD_OPS_AUTO_OOB;
  159. ops_pt.len = mtd->writesize;
  160. ops_pt.retlen = 0;
  161. ops_pt.ooblen = 16;
  162. ops_pt.oobretlen = 0;
  163. ops_pt.oobbuf = page_buf + page_size;
  164. ops_pt.ooboffs = 0;
  165. memset(page_buf, 0xFF, page_size + mtd->oobsize);
  166. memset(page_readbuf, 0xFF, page_size);
  167. /* mt6577_nand_erase(start_addr); //for test */
  168. #if defined(MTK_MLC_NAND_SUPPORT)
  169. for (page_offset = 0, i = 0; page_offset < (block_size / page_size);
  170. page_offset = functArray[devinfo.feature_set.ptbl_idx] (i++)) {
  171. #else
  172. for (page_offset = 0; page_offset < (block_size / page_size); page_offset++) {
  173. #endif
  174. current_add = start_addr + (page_offset * page_size);
  175. if (mtd->_read_oob(mtd, (loff_t) current_add, &ops_pt) != 0) {
  176. pr_notice("find_emp read failed %llx\n", current_add);
  177. continue;
  178. } else {
  179. if (memcmp(page_readbuf, page_buf, page_size)
  180. || memcmp(page_buf + page_size, page_readbuf, 32)) {
  181. continue;
  182. } else {
  183. pr_debug("find_emp at %x\n", page_offset);
  184. break;
  185. }
  186. }
  187. }
  188. pr_debug("find_emp find empty at %x\n", page_offset);
  189. /* i=(0x40); */
  190. /* pr_debug("test code %x\n",i); */
  191. /* page_offset = 0x40; */
  192. if (page_offset != 0x40) {
  193. pr_debug("find_emp at %x\n", page_offset);
  194. return page_offset;
  195. }
  196. pr_debug("find_emp no empty\n");
  197. ei.addr = start_addr;
  198. if (mtd->_erase(mtd, &ei) != 0) { /* no good block for used in replace pool */
  199. pr_notice("find_emp erase mirror failed %llx\n", start_addr);
  200. pi.mirror_pt_has_space = 0;
  201. return 0xFFFF;
  202. } else {
  203. return 0; /* the first page is empty */
  204. }
  205. }
  206. bool find_mirror_pt_from_bottom(u64 *start_addr, struct mtd_info *mtd)
  207. {
  208. int mpt_locate, i;
  209. u64 mpt_start_addr;
  210. u64 current_start_addr = 0;
  211. u8 pmt_spare[4];
  212. struct mtd_oob_ops ops_pt;
  213. mpt_start_addr = ((mtd->size) + block_size);
  214. /* mpt_start_addr=MPT_LOCATION*block_size-page_size; */
  215. memset(page_buf, 0xFF, page_size + mtd->oobsize);
  216. ops_pt.datbuf = (uint8_t *) page_buf;
  217. ops_pt.mode = MTD_OPS_AUTO_OOB;
  218. ops_pt.len = mtd->writesize;
  219. ops_pt.retlen = 0;
  220. ops_pt.ooblen = 16;
  221. ops_pt.oobretlen = 0;
  222. ops_pt.oobbuf = page_buf + page_size;
  223. ops_pt.ooboffs = 0;
  224. pr_debug("find_mirror find begain at %llx\n", mpt_start_addr);
  225. for (mpt_locate = ((block_size / page_size) - 1), i = ((block_size / page_size) - 1);
  226. mpt_locate >= 0; mpt_locate--) { /* mpt_locate--) */
  227. memset(pmt_spare, 0xFF, PT_SIG_SIZE);
  228. current_start_addr = mpt_start_addr + mpt_locate * page_size;
  229. if (mtd->_read_oob(mtd, (loff_t) current_start_addr, &ops_pt) != 0) {
  230. pr_notice("find_mirror read failed %llx %x\n", current_start_addr,
  231. mpt_locate);
  232. }
  233. memcpy(pmt_spare, &page_buf[page_size], PT_SIG_SIZE); /* auto do need skip bad block */
  234. /* need enhance must be the larget sequnce number */
  235. #if 0
  236. {
  237. int i;
  238. for (i = 0; i < 8; i++)
  239. pr_debug("%x %x\n", page_buf[i], page_buf[2048 + i]);
  240. }
  241. #endif
  242. if (is_valid_mpt(page_buf) && is_valid_mpt(pmt_spare)) {
  243. /* if no pt, pt.has space is 0; */
  244. pi.sequencenumber = page_buf[PT_SIG_SIZE + page_size];
  245. pr_debug("find_mirror find valid pt at %llx sq %x\n",
  246. current_start_addr, pi.sequencenumber);
  247. break;
  248. }
  249. continue;
  250. }
  251. if (mpt_locate == -1) {
  252. pr_notice("no valid mirror page\n");
  253. pi.sequencenumber = 0;
  254. return FALSE;
  255. }
  256. *start_addr = current_start_addr;
  257. return TRUE;
  258. }
  259. /* int load_exist_part_tab(u8 *buf,struct mtd_info *mtd) */
  260. int load_exist_part_tab(u8 *buf)
  261. {
  262. u64 pt_start_addr;
  263. u64 pt_cur_addr;
  264. int pt_locate, i;
  265. int reval = DM_ERR_OK;
  266. u64 mirror_address;
  267. /* u8 pmt_spare[PT_SIG_SIZE]; */
  268. struct mtd_oob_ops ops_pt;
  269. struct mtd_info *mtd;
  270. mtd = &host->mtd;
  271. block_size = mtd->erasesize; /* devinfo.blocksize*1024; */
  272. page_size = mtd->writesize; /* devinfo.pagesize; */
  273. /* if(host->hw->nand_sec_shift == 10) //MLC */
  274. /* block_size = block_size >> 1; */
  275. pt_start_addr = (mtd->size);
  276. /* pt_start_addr=PT_LOCATION*block_size; */
  277. pr_debug("load_exist_part_tab %llx\n", pt_start_addr);
  278. ops_pt.datbuf = (uint8_t *) page_buf;
  279. ops_pt.mode = MTD_OPS_AUTO_OOB;
  280. ops_pt.len = mtd->writesize;
  281. ops_pt.retlen = 0;
  282. ops_pt.ooblen = 16;
  283. ops_pt.oobretlen = 0;
  284. ops_pt.oobbuf = (page_buf + page_size);
  285. ops_pt.ooboffs = 0;
  286. pr_debug("ops_pt.len %lx\n", (unsigned long)ops_pt.len);
  287. if (mtd->_read_oob == NULL)
  288. pr_debug("should not happpen\n");
  289. for (pt_locate = 0, i = 0; pt_locate < (block_size / page_size); pt_locate++) {
  290. pt_cur_addr = pt_start_addr + pt_locate * page_size;
  291. /* memset(pmt_spare,0xFF,PT_SIG_SIZE); */
  292. /* pr_debug("load_pt read pt %x\n",pt_cur_addr); */
  293. if (mtd->_read_oob(mtd, (loff_t) pt_cur_addr, &ops_pt) != 0)
  294. pr_info("load_pt read pt failded: %llx\n", (u64) pt_cur_addr);
  295. #if 0
  296. {
  297. int i;
  298. for (i = 0; i < 8; i++) {
  299. pr_debug("%x %x\n", *(page_buf + i),
  300. *(page_buf + 2048 + i));
  301. }
  302. }
  303. #endif
  304. /* memcpy(pmt_spare,&page_buf[LPAGE] ,PT_SIG_SIZE); //do not need skip bad block flag */
  305. if (is_valid_pt(page_buf) && is_valid_pt(page_buf + mtd->writesize)) {
  306. pi.sequencenumber = page_buf[PT_SIG_SIZE + page_size];
  307. pr_info("load_pt find valid pt at %llx sq %x\n", pt_start_addr,
  308. pi.sequencenumber);
  309. break;
  310. }
  311. continue;
  312. }
  313. /* for test */
  314. /* pt_locate=(block_size/page_size); */
  315. if (pt_locate == (block_size / page_size)) {
  316. /* first download or download is not compelte after erase or can not download last time */
  317. pr_info("load_pt find pt failed\n");
  318. pi.pt_has_space = 0; /* or before download pt power lost */
  319. if (!find_mirror_pt_from_bottom(&mirror_address, mtd)) {
  320. pr_info("First time download\n");
  321. reval = ERR_NO_EXIST;
  322. return reval;
  323. }
  324. /* used the last valid mirror pt, at lease one is valid. */
  325. mtd->_read_oob(mtd, (loff_t) mirror_address, &ops_pt);
  326. }
  327. memcpy(&lastest_part, &page_buf[PT_SIG_SIZE], sizeof(lastest_part));
  328. return reval;
  329. }
  330. static int pmt_open(struct inode *inode, struct file *filp)
  331. {
  332. pr_debug("[%s]:(MAJOR)%d:(MINOR)%d\n", __func__, MAJOR(inode->i_rdev),
  333. MINOR(inode->i_rdev));
  334. /* filp->private_data = (int*); */
  335. return 0;
  336. }
  337. static int pmt_release(struct inode *inode, struct file *filp)
  338. {
  339. pr_debug("[%s]:(MAJOR)%d:(MINOR)%d\n", __func__, MAJOR(inode->i_rdev),
  340. MINOR(inode->i_rdev));
  341. return 0;
  342. }
  343. static long pmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  344. {
  345. long ret = 0; /* , i=0; */
  346. ulong version = PT_SIG;
  347. void __user *uarg = (void __user *)arg;
  348. pr_debug("PMT IOCTL: Enter\n");
  349. if (false == g_bInitDone) {
  350. pr_err("ERROR: NAND Flash Not initialized !!\n");
  351. ret = -EFAULT;
  352. goto exit;
  353. }
  354. switch (cmd) {
  355. case PMT_READ:
  356. pr_debug("PMT IOCTL: PMT_READ\n");
  357. ret = read_pmt(uarg);
  358. break;
  359. case PMT_WRITE:
  360. pr_debug("PMT IOCTL: PMT_WRITE\n");
  361. if (copy_from_user(&pmtctl, uarg, sizeof(DM_PARTITION_INFO_PACKET))) {
  362. ret = -EFAULT;
  363. goto exit;
  364. }
  365. new_part_tab((u8 *) &pmtctl, (struct mtd_info *)&host->mtd);
  366. update_part_tab((struct mtd_info *)&host->mtd);
  367. break;
  368. case PMT_VERSION:
  369. if (copy_to_user((void __user *)arg, &version, PT_SIG_SIZE))
  370. ret = -EFAULT;
  371. else
  372. ret = 0;
  373. break;
  374. default:
  375. ret = -EINVAL;
  376. }
  377. exit:
  378. return ret;
  379. }
  380. static int read_pmt(void __user *arg)
  381. {
  382. pr_debug("read_pmt\n");
  383. if (copy_to_user(arg, &lastest_part, sizeof(pt_resident) * PART_MAX_COUNT))
  384. return -EFAULT;
  385. return 0;
  386. }
  387. static const struct file_operations pmt_fops = {
  388. .owner = THIS_MODULE,
  389. .unlocked_ioctl = pmt_ioctl,
  390. .open = pmt_open,
  391. .release = pmt_release,
  392. };
  393. static struct miscdevice pmt_dev = {
  394. .minor = MISC_DYNAMIC_MINOR,
  395. .name = "pmt",
  396. .fops = &pmt_fops,
  397. };
  398. static int lowercase(int c)
  399. {
  400. if ((c >= 'A') && (c <= 'Z'))
  401. c += 'a' - 'A';
  402. return c;
  403. }
  404. void construct_mtd_partition(struct mtd_info *mtd)
  405. {
  406. int i, j;
  407. for (i = 0; i < PART_MAX_COUNT; i++) {
  408. /* if((lastest_part[i].size == 0) && (strcmp(lastest_part[i].name,"BMTPOOL"))) */
  409. /* break; */
  410. if (!strcmp(lastest_part[i - 1].name, "BMTPOOL"))
  411. break;
  412. for (j = 0; j < MAX_PARTITION_NAME_LEN; j++) {
  413. if (lastest_part[i].name[j] == 0)
  414. break;
  415. part_name[i][j] = lowercase(lastest_part[i].name[j]);
  416. }
  417. PartInfo[i].name = part_name[i];
  418. g_exist_Partition[i].name = part_name[i];
  419. if (!strcmp(lastest_part[i].name, "SECCFG"))
  420. g_exist_Partition[i].name = MTD_SECFG_STR;
  421. if (!strcmp(lastest_part[i].name, "BOOTIMG"))
  422. g_exist_Partition[i].name = MTD_BOOTIMG_STR;
  423. if (!strcmp(lastest_part[i].name, "SEC_RO"))
  424. g_exist_Partition[i].name = MTD_SECRO_STR;
  425. if (!strcmp(lastest_part[i].name, "ANDROID"))
  426. g_exist_Partition[i].name = MTD_ANDROID_STR;
  427. if (!strcmp(lastest_part[i].name, "USRDATA"))
  428. g_exist_Partition[i].name = MTD_USRDATA_STR;
  429. g_exist_Partition[i].size = (uint64_t) lastest_part[i].size; /* mtd partition */
  430. g_exist_Partition[i].offset = (uint64_t) lastest_part[i].offset;
  431. g_exist_Partition[i].mask_flags = lastest_part[i].mask_flags;
  432. /* PartInfo[i].name = part_name[i]; //dumchar */
  433. PartInfo[i].type = NAND;
  434. PartInfo[i].start_address = lastest_part[i].offset;
  435. PartInfo[i].size = lastest_part[i].size;
  436. partition_type_array[i] = lastest_part[i].part_id;
  437. pr_debug("partition %s %s size %llx\n", lastest_part[i].name, PartInfo[i].name,
  438. g_exist_Partition[i].offset);
  439. #if 1
  440. if (MLC_DEVICE == TRUE) {
  441. mtd->eraseregions[i].offset = lastest_part[i].offset;
  442. mtd->eraseregions[i].erasesize = mtd->erasesize;
  443. if (partition_type_array[i] == REGION_LOW_PAGE)
  444. mtd->eraseregions[i].erasesize = mtd->erasesize / 2;
  445. mtd->numeraseregions++;
  446. }
  447. #endif
  448. }
  449. part_num = i;
  450. g_exist_Partition[i - 1].size = MTDPART_SIZ_FULL;
  451. }
  452. void part_init_pmt(struct mtd_info *mtd, u8 *buf)
  453. {
  454. /* struct mtd_partition *part; */
  455. /* u64 lastblk; */
  456. int retval = 0;
  457. int i = 0;
  458. int err = 0;
  459. pr_debug("part_init_pmt\n");
  460. page_buf = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
  461. page_readbuf = kzalloc(mtd->writesize, GFP_KERNEL);
  462. #if 0
  463. part = &g_pasStatic_Partition[0];
  464. lastblk = part->offset + part->size;
  465. pr_debug("offset %llx part->size %llx %s\n", part->offset, part->size, part->name);
  466. while (1) {
  467. part++;
  468. if (part->offset == MTDPART_OFS_APPEND)
  469. part->offset = lastblk;
  470. lastblk = part->offset + part->size;
  471. pr_debug("mt_part_init_pmt %llx\n", part->offset);
  472. if (part->size == 0) { /* //the last partition sizefull ==0 */
  473. break;
  474. }
  475. }
  476. #endif
  477. memset(&pi, 0xFF, sizeof(pi));
  478. memset(&lastest_part, 0, PART_MAX_COUNT * sizeof(pt_resident));
  479. retval = load_exist_part_tab(buf);
  480. if (retval == ERR_NO_EXIST) {/* first run preloader before dowload */
  481. /* and valid mirror last download or first download */
  482. pr_notice("%s no pt\n", __func__);
  483. get_part_tab_from_complier(); /* get from complier */
  484. if (MLC_DEVICE == TRUE)
  485. mtd->numeraseregions = 0;
  486. for (i = 0; i < part_num; i++) {
  487. #if 1
  488. if (MLC_DEVICE == TRUE) {
  489. mtd->eraseregions[i].offset = lastest_part[i].offset;
  490. mtd->eraseregions[i].erasesize = mtd->erasesize;
  491. if (partition_type_array[i] == REGION_LOW_PAGE)
  492. mtd->eraseregions[i].erasesize = mtd->erasesize / 2;
  493. mtd->numeraseregions++;
  494. }
  495. #endif
  496. }
  497. } else {
  498. pr_debug("Find pt or mpt\n");
  499. if (MLC_DEVICE == TRUE)
  500. mtd->numeraseregions = 0;
  501. #if 0
  502. memcpy(&g_exist_Partition, &g_pasStatic_Partition,
  503. sizeof(struct mtd_partition) * part_num);
  504. for (i = 0; i < part_num; i++) {
  505. pr_debug("partition %s size %llx %llx\n", lastest_part[i].name,
  506. lastest_part[i].offset, lastest_part[i].size);
  507. /* still use the name in g_pasSatic_partition */
  508. g_exist_Partition[i].size = lastest_part[i].size;
  509. g_exist_Partition[i].offset = lastest_part[i].offset;
  510. #if 1
  511. if (MLC_DEVICE == TRUE) {
  512. mtd->eraseregions[i].offset = lastest_part[i].offset;
  513. mtd->eraseregions[i].erasesize = mtd->erasesize;
  514. #if 0 /* to build pass xiaolei */
  515. if (partition_type_array[i] == TYPE_RAW)
  516. mtd->eraseregions[i].erasesize = mtd->erasesize / 2;
  517. #endif
  518. mtd->numeraseregions++;
  519. }
  520. #endif
  521. /* still use the mask flag in g_pasSatic_partition */
  522. if (i == (part_num - 1))
  523. g_exist_Partition[i].size = MTDPART_SIZ_FULL;
  524. pr_debug("partition %s size %llx\n", lastest_part[i].name,
  525. g_exist_Partition[i].offset);
  526. }
  527. #endif
  528. construct_mtd_partition(mtd);
  529. }
  530. init_pmt_done = TRUE;
  531. pr_debug(": register NAND PMT device ...\n");
  532. #ifndef MTK_EMMC_SUPPORT
  533. err = misc_register(&pmt_dev);
  534. if (unlikely(err)) {
  535. pr_err("PMT failed to register device!\n");
  536. /* return err; */
  537. }
  538. #endif
  539. }
  540. int new_part_tab(u8 *buf, struct mtd_info *mtd)
  541. {
  542. DM_PARTITION_INFO_PACKET *dm_part = (DM_PARTITION_INFO_PACKET *) buf;
  543. int part_num, change_index, i = 0;
  544. int retval;
  545. int pageoffset;
  546. u64 start_addr = (u64) ((mtd->size) + block_size);
  547. u64 current_addr = 0;
  548. struct mtd_oob_ops ops_pt;
  549. pi.pt_changed = 0;
  550. pi.tool_or_sd_update = 2; /* tool download is 1. */
  551. ops_pt.mode = MTD_OPS_AUTO_OOB;
  552. ops_pt.len = mtd->writesize;
  553. ops_pt.retlen = 0;
  554. ops_pt.ooblen = 16;
  555. ops_pt.oobretlen = 0;
  556. ops_pt.oobbuf = page_buf + page_size;
  557. ops_pt.ooboffs = 0;
  558. /* the first image is ? */
  559. #if 1
  560. for (part_num = 0; part_num < PART_MAX_COUNT; part_num++) {
  561. memcpy(new_part[part_num].name, dm_part->part_info[part_num].part_name,
  562. MAX_PARTITION_NAME_LEN);
  563. new_part[part_num].offset = dm_part->part_info[part_num].start_addr;
  564. new_part[part_num].size = dm_part->part_info[part_num].part_len;
  565. new_part[part_num].mask_flags = 0;
  566. /* MSG (INIT, "DM_PARTITION_INFO_PACKET %s size %x %x\n",
  567. dm_part->part_info[part_num].part_name,
  568. dm_part->part_info[part_num].part_len,part_num); */
  569. pr_debug("new_pt %s size %llx\n", new_part[part_num].name,
  570. new_part[part_num].size);
  571. if (dm_part->part_info[part_num].part_len == 0) {
  572. pr_debug("new_pt last %x\n", part_num);
  573. break;
  574. }
  575. }
  576. #endif
  577. /* ++++++++++for test */
  578. #if 0
  579. part_num = 13;
  580. memcpy(&new_part[0], &lastest_part[0], sizeof(new_part));
  581. MSG(INIT, "new_part %x size\n", sizeof(new_part));
  582. for (i = 0; i < part_num; i++) {
  583. MSG(INIT, "npt partition %s size\n", new_part[i].name);
  584. /* MSG (INIT, "npt %x size\n",new_part[i].offset); */
  585. /* MSG (INIT, "npt %x size\n",lastest_part[i].offset); */
  586. /* MSG (INIT, "npt %x size\n",new_part[i].size); */
  587. dm_part->part_info[5].part_visibility = 1;
  588. dm_part->part_info[5].dl_selected = 1;
  589. new_part[5].size = lastest_part[5].size + 0x100000;
  590. }
  591. #endif
  592. /* ------------for test */
  593. /* Find the first changed partition, whether is visible */
  594. for (change_index = 0; change_index <= part_num; change_index++) {
  595. if ((new_part[change_index].size != lastest_part[change_index].size)
  596. || (new_part[change_index].offset != lastest_part[change_index].offset)) {
  597. pr_debug("new_pt %x size changed from %llx to %llx\n", change_index,
  598. lastest_part[change_index].size, new_part[change_index].size);
  599. pi.pt_changed = 1;
  600. break;
  601. }
  602. }
  603. if (pi.pt_changed == 1) {
  604. /* Is valid image update */
  605. for (i = change_index; i <= part_num; i++) {
  606. if (dm_part->part_info[i].dl_selected == 0
  607. && dm_part->part_info[i].part_visibility == 1) {
  608. pr_notice("Full download is need %x\n", i);
  609. retval = DM_ERR_NO_VALID_TABLE;
  610. return retval;
  611. }
  612. }
  613. pageoffset = find_empty_page_from_top(start_addr, mtd);
  614. /* download partition used the new partition */
  615. /* write mirror at the same 2 page */
  616. memset(page_buf, 0xFF, page_size + 64);
  617. *(int *)sig_buf = MPT_SIG;
  618. memcpy(page_buf, &sig_buf, PT_SIG_SIZE);
  619. memcpy(&page_buf[PT_SIG_SIZE], &new_part[0], sizeof(new_part));
  620. memcpy(&page_buf[page_size], &sig_buf, PT_SIG_SIZE);
  621. pi.sequencenumber += 1;
  622. memcpy(&page_buf[page_size + PT_SIG_SIZE], &pi, PT_SIG_SIZE);
  623. if (pageoffset != 0xFFFF) {
  624. if ((pageoffset % 2) != 0) {
  625. pr_info("new_pt mirror block may destroy last time%x\n",
  626. pageoffset);
  627. pageoffset += 1;
  628. }
  629. for (i = 0; i < 2; i++) {
  630. current_addr = start_addr + (pageoffset + i) * page_size;
  631. ops_pt.datbuf = (uint8_t *) page_buf;
  632. if (mtd->_write_oob(mtd, (loff_t) current_addr, &ops_pt) != 0) {
  633. pr_info("new_pt write m first page failed %llx\n",
  634. current_addr);
  635. continue;
  636. }
  637. pr_info("new_pt write mirror at %llx\n",
  638. current_addr);
  639. ops_pt.datbuf = (uint8_t *) page_readbuf;
  640. /* read back verify */
  641. if ((mtd->_read_oob(mtd, (loff_t) current_addr, &ops_pt) !=
  642. 0) || memcmp(page_buf, page_readbuf, page_size)) {
  643. pr_info("new_pt read or verify first mirror page failed %llx\n",
  644. current_addr);
  645. ops_pt.datbuf = (uint8_t *) page_buf;
  646. memset(page_buf, 0, PT_SIG_SIZE);
  647. if (mtd->_read_oob(mtd, (loff_t) current_addr, &ops_pt) != 0)
  648. pr_info("new_pt mark failed %llx\n", current_addr);
  649. } else {
  650. pr_info("new_pt write mirror ok %x\n", i);
  651. /* any one success set this flag? */
  652. pi.mirror_pt_dl = 1;
  653. }
  654. }
  655. }
  656. } else {
  657. pr_debug("new_part_tab no pt change %x\n", i);
  658. }
  659. retval = DM_ERR_OK;
  660. return retval;
  661. }
  662. int update_part_tab(struct mtd_info *mtd)
  663. {
  664. int retval = 0;
  665. int retry_w;
  666. int retry_r;
  667. u64 start_addr = (u64) (mtd->size); /* PT_LOCATION*block_size; */
  668. u64 current_addr = 0;
  669. struct erase_info ei;
  670. struct mtd_oob_ops ops_pt;
  671. memset(page_buf, 0xFF, page_size + 64);
  672. ei.mtd = mtd;
  673. ei.len = mtd->erasesize;
  674. ei.time = 1000;
  675. ei.retries = 2;
  676. ei.callback = NULL;
  677. ops_pt.mode = MTD_OPS_AUTO_OOB;
  678. ops_pt.len = mtd->writesize;
  679. ops_pt.retlen = 0;
  680. ops_pt.ooblen = 16;
  681. ops_pt.oobretlen = 0;
  682. ops_pt.oobbuf = page_buf + page_size;
  683. ops_pt.ooboffs = 0;
  684. if ((pi.pt_changed == 1 || pi.pt_has_space == 0) && pi.tool_or_sd_update == 2) {
  685. pr_debug("update_pt pt changes\n");
  686. ei.addr = start_addr;
  687. if (mtd->_erase(mtd, &ei) != 0) { /* no good block for used in replace pool */
  688. pr_err("update_pt erase failed %llx\n", start_addr);
  689. if (pi.mirror_pt_dl == 0)
  690. retval = DM_ERR_NO_SPACE_FOUND;
  691. return retval;
  692. }
  693. for (retry_r = 0; retry_r < RETRY_TIMES; retry_r++) {
  694. for (retry_w = 0; retry_w < RETRY_TIMES; retry_w++) {
  695. current_addr = start_addr + (retry_w + retry_r * RETRY_TIMES) * page_size;
  696. *(int *)sig_buf = PT_SIG;
  697. memcpy(page_buf, &sig_buf, PT_SIG_SIZE);
  698. memcpy(&page_buf[PT_SIG_SIZE], &new_part[0], sizeof(new_part));
  699. memcpy(&page_buf[page_size], &sig_buf, PT_SIG_SIZE);
  700. memcpy(&page_buf[page_size + PT_SIG_SIZE], &pi, PT_SIG_SIZE);
  701. ops_pt.datbuf = (uint8_t *) page_buf;
  702. /* no good block for used in replace pool . still used the original ones */
  703. if (mtd->_write_oob(mtd, (loff_t) current_addr, &ops_pt) != 0) {
  704. pr_notice("update_pt write failed %x\n", retry_w);
  705. memset(page_buf, 0, PT_SIG_SIZE);
  706. if (mtd->_write_oob(mtd, (loff_t) current_addr, &ops_pt) !=
  707. 0) {
  708. pr_notice("write error mark failed\n");
  709. /* continue retry */
  710. continue;
  711. }
  712. } else {
  713. pr_debug("write pt success %llx %x\n",
  714. current_addr, retry_w);
  715. break; /* retry_w should not count. */
  716. }
  717. }
  718. if (retry_w == RETRY_TIMES) {
  719. pr_notice("update_pt retry w failed\n");
  720. if (pi.mirror_pt_dl == 0) { /* mirror also can not write down */
  721. retval = DM_ERR_NO_SPACE_FOUND;
  722. return retval;
  723. } else {
  724. return DM_ERR_OK;
  725. }
  726. }
  727. current_addr = (start_addr + (((retry_w) + retry_r * RETRY_TIMES) * page_size));
  728. ops_pt.datbuf = (uint8_t *) page_readbuf;
  729. if ((mtd->_read_oob(mtd, (loff_t) current_addr, &ops_pt) != 0)
  730. || memcmp(page_buf, page_readbuf, page_size)) {
  731. pr_debug("v or r failed %x\n", retry_r);
  732. memset(page_buf, 0, PT_SIG_SIZE);
  733. ops_pt.datbuf = (uint8_t *) page_buf;
  734. if (mtd->_write_oob(mtd, (loff_t) current_addr, &ops_pt) != 0) {
  735. pr_notice("read error mark failed\n");
  736. /* continue retryp */
  737. continue;
  738. }
  739. } else {
  740. pr_debug("update_pt r&v ok%llx\n", current_addr);
  741. break;
  742. }
  743. }
  744. } else {
  745. pr_debug("update_pt no change\n");
  746. }
  747. return DM_ERR_OK;
  748. }
  749. int get_part_num_nand(void)
  750. {
  751. return part_num;
  752. }
  753. EXPORT_SYMBOL(get_part_num_nand);
  754. #endif