-int main(int argc, char **argv) {
- if(argc == 3 && strcasecmp(argv[1],"unlock")==0) {
- printf("Unlocking %s ...\n",argv[2]);
- return mtd_unlock(argv[2]);
+void usage(void)
+{
+ printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
+ "The device is in the format of mtdX (eg: mtd4) or its label.\n"
+ "mtd recognizes these commands:\n"
+ " unlock unlock the device\n"
+ " erase erase all data on device\n"
+ " write <imagefile> write imagefile to device\n"
+ "Following options are available:\n"
+ " -r reboot after successful command\n"
+ " -e <device> erase <device> before executing the command\n\n"
+ "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
+ " mtd -r write linux.trx linux\n\n");
+ exit(1);
+}
+
+int main (int argc, char **argv)
+{
+ int ch, i, boot, unlock;
+ char *erase[MAX_ARGS], *device;
+ enum {
+ CMD_ERASE,
+ CMD_WRITE,
+ CMD_UNLOCK
+ } cmd;
+
+ erase[0] = NULL;
+ boot = 0;
+
+ while ((ch = getopt(argc, argv, "re:")) != -1)
+ switch (ch) {
+ case 'r':
+ boot = 1;
+ break;
+ case 'e':
+ i = 0;
+ while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
+ i++;
+
+ erase[i++] = optarg;
+ erase[i] = NULL;
+ break;
+
+ case '?':
+ default:
+ usage();
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 2)
+ usage();
+
+ if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
+ cmd = CMD_UNLOCK;
+ device = argv[1];
+ } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
+ cmd = CMD_ERASE;
+ device = argv[1];
+ } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
+ cmd = CMD_WRITE;
+ device = argv[2];
+ } else {
+ usage();