trxsplit.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) Gabor Juhos <juhosg@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/list.h>
  14. #include <linux/kmod.h>
  15. #include <linux/root_dev.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <linux/byteorder/generic.h>
  19. #define PFX "trxsplit: "
  20. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  21. #define TRX_VERSION 1
  22. #define TRX_MAX_LEN 0x3A0000
  23. #define TRX_NO_HEADER 0x1 /* do not write TRX header */
  24. #define TRX_GZ_FILES 0x2 /* contains individual gzip files */
  25. #define TRX_MAX_OFFSET 3
  26. #define TRX_MIN_KERNEL_SIZE (256 * 1024)
  27. struct trx_header {
  28. u32 magic; /* "HDR0" */
  29. u32 len; /* Length of file including header */
  30. u32 crc32; /* 32-bit CRC from flag_version to end of file */
  31. u32 flag_version; /* 0:15 flags, 16:31 version */
  32. u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions */
  33. };
  34. #define TRX_ALIGN 0x1000
  35. static int trx_nr_parts;
  36. static unsigned long trx_offset;
  37. static struct mtd_info *trx_mtd;
  38. static struct mtd_partition trx_parts[TRX_MAX_OFFSET];
  39. static struct trx_header trx_hdr;
  40. static int trxsplit_refresh_partitions(struct mtd_info *mtd);
  41. static int trxsplit_checktrx(struct mtd_info *mtd, unsigned long offset)
  42. {
  43. size_t retlen;
  44. int err;
  45. err = mtd_read(mtd, offset, sizeof(trx_hdr), &retlen, (void *)&trx_hdr);
  46. if (err) {
  47. printk(KERN_ALERT PFX "unable to read from '%s'\n", mtd->name);
  48. goto err_out;
  49. }
  50. if (retlen != sizeof(trx_hdr)) {
  51. printk(KERN_ALERT PFX "reading failed on '%s'\n", mtd->name);
  52. goto err_out;
  53. }
  54. trx_hdr.magic = le32_to_cpu(trx_hdr.magic);
  55. trx_hdr.len = le32_to_cpu(trx_hdr.len);
  56. trx_hdr.crc32 = le32_to_cpu(trx_hdr.crc32);
  57. trx_hdr.flag_version = le32_to_cpu(trx_hdr.flag_version);
  58. trx_hdr.offsets[0] = le32_to_cpu(trx_hdr.offsets[0]);
  59. trx_hdr.offsets[1] = le32_to_cpu(trx_hdr.offsets[1]);
  60. trx_hdr.offsets[2] = le32_to_cpu(trx_hdr.offsets[2]);
  61. /* sanity checks */
  62. if (trx_hdr.magic != TRX_MAGIC)
  63. goto err_out;
  64. if (trx_hdr.len > mtd->size - offset)
  65. goto err_out;
  66. /* TODO: add crc32 checking too? */
  67. return 0;
  68. err_out:
  69. return -1;
  70. }
  71. static void trxsplit_findtrx(struct mtd_info *mtd)
  72. {
  73. unsigned long offset;
  74. int err;
  75. printk(KERN_INFO PFX "searching TRX header in '%s'\n", mtd->name);
  76. err = 0;
  77. for (offset = 0; offset < mtd->size; offset += TRX_ALIGN) {
  78. err = trxsplit_checktrx(mtd, offset);
  79. if (err == 0)
  80. break;
  81. }
  82. if (err)
  83. return;
  84. printk(KERN_INFO PFX "TRX header found at 0x%lX\n", offset);
  85. trx_mtd = mtd;
  86. trx_offset = offset;
  87. }
  88. static void trxsplit_create_partitions(struct mtd_info *mtd)
  89. {
  90. struct mtd_partition *part = trx_parts;
  91. int err;
  92. int i;
  93. for (i = 0; i < TRX_MAX_OFFSET; i++) {
  94. part = &trx_parts[i];
  95. if (trx_hdr.offsets[i] == 0)
  96. continue;
  97. part->offset = trx_offset + trx_hdr.offsets[i];
  98. trx_nr_parts++;
  99. }
  100. for (i = 0; i < trx_nr_parts-1; i++)
  101. trx_parts[i].size = trx_parts[i+1].offset - trx_parts[i].offset;
  102. trx_parts[i].size = mtd->size - trx_parts[i].offset;
  103. i = 0;
  104. part = &trx_parts[i];
  105. if (part->size < TRX_MIN_KERNEL_SIZE) {
  106. part->name = "loader";
  107. i++;
  108. }
  109. part = &trx_parts[i];
  110. part->name = "kernel";
  111. i++;
  112. part = &trx_parts[i];
  113. part->name = "rootfs";
  114. err = mtd_device_register(mtd, trx_parts, trx_nr_parts);
  115. if (err) {
  116. printk(KERN_ALERT PFX "adding TRX partitions failed\n");
  117. return;
  118. }
  119. mtd->refresh_device = trxsplit_refresh_partitions;
  120. }
  121. static int trxsplit_refresh_partitions(struct mtd_info *mtd)
  122. {
  123. printk(KERN_INFO PFX "refreshing TRX partitions in '%s' (%d,%d)\n",
  124. mtd->name, MTD_BLOCK_MAJOR, mtd->index);
  125. /* remove old partitions */
  126. mtd_device_unregister(mtd);
  127. trxsplit_findtrx(mtd);
  128. if (!trx_mtd)
  129. goto err;
  130. trxsplit_create_partitions(trx_mtd);
  131. return 1;
  132. err:
  133. return 0;
  134. }
  135. static void __init trxsplit_add_mtd(struct mtd_info *mtd)
  136. {
  137. if (mtd->type != MTD_NORFLASH) {
  138. printk(KERN_INFO PFX "'%s' is not a NOR flash, skipped\n",
  139. mtd->name);
  140. return;
  141. }
  142. if (!trx_mtd)
  143. trxsplit_findtrx(mtd);
  144. }
  145. static void __init trxsplit_remove_mtd(struct mtd_info *mtd)
  146. {
  147. /* nothing to do */
  148. }
  149. static struct mtd_notifier trxsplit_notifier __initdata = {
  150. .add = trxsplit_add_mtd,
  151. .remove = trxsplit_remove_mtd,
  152. };
  153. static void __init trxsplit_scan(void)
  154. {
  155. register_mtd_user(&trxsplit_notifier);
  156. unregister_mtd_user(&trxsplit_notifier);
  157. }
  158. static int __init trxsplit_init(void)
  159. {
  160. trxsplit_scan();
  161. if (trx_mtd) {
  162. printk(KERN_INFO PFX "creating TRX partitions in '%s' "
  163. "(%d,%d)\n", trx_mtd->name, MTD_BLOCK_MAJOR,
  164. trx_mtd->index);
  165. trxsplit_create_partitions(trx_mtd);
  166. }
  167. return 0;
  168. }
  169. late_initcall(trxsplit_init);