load_nandsim.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/sh -euf
  2. #
  3. # This script inserts NAND simulator module to emulate NAND flash of specified
  4. # size.
  5. #
  6. # Author: Artem Bityutskiy
  7. #
  8. fatal()
  9. {
  10. echo "Error: $1" 1>&2
  11. exit 1
  12. }
  13. usage()
  14. {
  15. cat 1>&2 <<EOF
  16. Load NAND simulator to simulate flash of a specified size.
  17. Usage: ${0##*/} <size in MiB> <eraseblock size in KiB> \\
  18. <page size (512 or 2048)>
  19. Only the first parameter is mandatory. Default eraseblock size
  20. is 16KiB, default NAND page size is 512 bytes.
  21. Only the following combinations are supported:
  22. --------------------------------------------------
  23. | size (MiB) | EB size (KiB) | Page size (bytes) |
  24. --------------------------------------------------
  25. | 16 | 16 | 512 |
  26. | 32 | 16 | 512 |
  27. | 64 | 16 | 512 |
  28. | 128 | 16 | 512 |
  29. | 256 | 16 | 512 |
  30. | 64 | 64 | 2048 |
  31. | 64 | 128 | 2048 |
  32. | 64 | 256 | 2048 |
  33. | 64 | 512 | 2048 |
  34. | 128 | 64 | 2048 |
  35. | 128 | 128 | 2048 |
  36. | 128 | 256 | 2048 |
  37. | 128 | 512 | 2048 |
  38. | 256 | 64 | 2048 |
  39. | 256 | 128 | 2048 |
  40. | 256 | 256 | 2048 |
  41. | 256 | 512 | 2048 |
  42. | 512 | 64 | 2048 |
  43. | 512 | 128 | 2048 |
  44. | 512 | 256 | 2048 |
  45. | 512 | 512 | 2048 |
  46. | 1024 | 64 | 2048 |
  47. | 1024 | 128 | 2048 |
  48. | 1024 | 256 | 2048 |
  49. | 1024 | 512 | 2048 |
  50. --------------------------------------------------
  51. EOF
  52. }
  53. if grep -q "NAND simulator" /proc/mtd; then
  54. fatal "nandsim is already loaded"
  55. fi
  56. if [ "$#" -lt "1" ]; then
  57. usage
  58. exit 1
  59. fi
  60. size="$1"
  61. eb_size="$2"
  62. page_size="$3"
  63. if [ "$#" = "1" ]; then
  64. eb_size="16"
  65. page_size="512"
  66. elif [ "$#" = "2" ]; then
  67. page_size="512"
  68. fi
  69. if [ "$page_size" -eq 512 ] && [ "$eb_size" -ne "16" ]; then
  70. fatal "only 16KiB eraseblocks are possible in case of 512 bytes page"
  71. fi
  72. first=
  73. second=
  74. third=
  75. fourth=
  76. if [ "$page_size" -eq "512" ]; then
  77. first="0x20"
  78. case "$size" in
  79. 16) second=0x33 ;;
  80. 32) second=0x35 ;;
  81. 64) second=0x36 ;;
  82. 128) second=0x78 ;;
  83. 256) second=0x71 ;;
  84. *) fatal "flash size ${size}MiB is not supported, try 16, 32, 64 or 256"
  85. esac
  86. elif [ "$page_size" -eq "2048" ]; then
  87. case "$eb_size" in
  88. 64) fourth="0x05" ;;
  89. 128) fourth="0x15" ;;
  90. 256) fourth="0x25" ;;
  91. 512) fourth="0x35" ;;
  92. *) fatal "eraseblock ${eb_size}KiB is not supported"
  93. esac
  94. case "$size" in
  95. 64) first="0x20"; second="0xa2"; third="0x00 ";;
  96. 128) first="0xec"; second="0xa1"; third="0x00 ";;
  97. 256) first="0x20"; second="0xaa"; third="0x00 ";;
  98. 512) first="0x20"; second="0xac"; third="0x00 ";;
  99. 1024) first="0xec"; second="0xd3"; third="0x51 ";;
  100. *) fatal "unable to emulate ${size}MiB flash with ${eb_size}KiB eraseblock"
  101. esac
  102. else
  103. fatal "bad NAND page size ${page_size}KiB, it has to be either 512 or 2048"
  104. fi
  105. first="first_id_byte=$first"
  106. second="second_id_byte=$second"
  107. [ -z "$third" ] || third="third_id_byte=$third"
  108. [ -z "$fourth" ] || fourth="fourth_id_byte=$fourth"
  109. modprobe nandsim "$first" "$second" $third $fourth
  110. echo "Loaded NAND simulator (${size}MiB, ${eb_size}KiB eraseblock, $page_size bytes NAND page)"