Mindspeed's patch to add a "-a" option to loady.

This allows the ymodem downloader to download straight to a memory location
instead of a file.  Useful in the microloader so it doesn't have to have a
filesystem.

Note: I modified this patch from the original, which required -a and -o to
be provided separately.  If you didn't provide -o, the default offset was 0,
which was pretty useless (and confusing since in "loady -a 0x01000000" the
second parameter was completely ignored).  Now the offset is mandatory so
it's harder to mess up.

Change-Id: I610ca731d806a51752ab711db4c98c16feab449d
diff --git a/commands/loadb.c b/commands/loadb.c
index 4c27bd6..3bc16ae 100644
--- a/commands/loadb.c
+++ b/commands/loadb.c
@@ -600,6 +600,10 @@
 #endif				/* CONFIG_CMD_LOADB */
 
 #ifdef CONFIG_CMD_LOADY
+
+unsigned long dst_ddr_address = 0x0;
+int dst_ddr = 0;
+
 /**
  * @brief getcxmodem
  *
@@ -625,7 +629,10 @@
 	int res, wr;
 	connection_info_t info;
 	char ymodemBuf[1024];
-	ulong addr = 0;
+	void *addr = 0;
+
+	if (dst_ddr)
+		addr = (void *)dst_ddr_address;
 
 	size = 0;
 	info.mode = xyzModem_ymodem;
@@ -634,7 +641,13 @@
 		while ((res = xyzModem_stream_read(ymodemBuf, 1024, &err)) >
 				0) {
 			size += res;
-			addr += res;
+
+			if (dst_ddr) {
+				memcpy(addr, ymodemBuf, res);
+				addr += res;
+				continue;
+			}
+
 			wr = write(ofd, ymodemBuf, res);
 			if (res != wr) {
 				perror("ymodem");
@@ -697,7 +710,7 @@
 	char *output_file = NULL;
 	struct console_device *cdev = NULL;
 
-	while ((opt = getopt(argc, argv, "f:b:o:c")) > 0) {
+	while ((opt = getopt(argc, argv, "f:b:o:ca:")) > 0) {
 		switch (opt) {
 		case 'f':
 			output_file = optarg;
@@ -711,6 +724,15 @@
 		case 'c':
 			open_mode |= O_CREAT;
 			break;
+		case 'a':
+			dst_ddr = 1;
+			offset = (int)simple_strtoul(optarg, NULL, 10);
+			if (optarg[0] == '-') {
+				printk("%s: bad offset '%s'\n",
+					argv[0], optarg);
+				return 1;
+			}
+			break;
 		default:
 			printk("%s: unknown option %c\n", argv[0], opt);
 			return 1;
@@ -730,6 +752,11 @@
 	if (NULL == output_file)
 		output_file = DEF_FILE;
 
+	if (dst_ddr) {
+		dst_ddr_address = offset;
+		goto skip_file_open;
+	}
+
 	/* File should exist */
 	ofd = open(output_file, open_mode);
 	if (ofd < 0) {
@@ -747,6 +774,7 @@
 		}
 	}
 
+skip_file_open:
 	if (load_baudrate != current_baudrate) {
 		printf("## Switch baudrate to %d bps and press ENTER ...\n",
 		       load_baudrate);
@@ -760,9 +788,14 @@
 	}
 #ifdef CONFIG_CMD_LOADY
 	if (strcmp(argv[0], "loady") == 0) {
-		printf("## Ready for binary (ymodem) download "
-		       "to 0x%08lX offset on %s device at %d bps...\n", offset,
-		       output_file, load_baudrate);
+		if (dst_ddr) {
+			printf("## Ready for binary (ymodem) download "
+				"to 0x%08lX on DDR at %d bps...\n", dst_ddr_address, load_baudrate);
+		} else {
+			printf("## Ready for binary (ymodem) download "
+				"to 0x%08lX offset on %s device at %d bps...\n", offset,
+			output_file, load_baudrate);
+		}
 		addr = load_serial_ymodem();
 	}
 #endif
@@ -791,18 +824,24 @@
 		}
 	}
 
-	close(ofd);
-	ofd = 0;
+	if (dst_ddr) {
+		dst_ddr = 0;
+	} else {
+		close(ofd);
+		ofd = 0;
+	}
+
 	return rcode;
 }
 
 static const __maybe_unused char cmd_loadb_help[] =
     "[OPTIONS]\n"
     "  -f file   - where to download to - defaults to " DEF_FILE "\n"
-    "  -o offset - what offset to download - defaults to 0\n"
+    "  -o offset - what offset in file to download - defaults to 0\n"
     "  -b baud   - baudrate at which to download - defaults to "
     "console baudrate\n"
-    "  -c        - Create file if it is not present - default disabled";
+    "  -c        - Create file if it is not present - default enabled\n"
+    "  -a offset - upload to DRAM address <offset> instead of a file\n";
 #ifdef CONFIG_CMD_LOADB
 BAREBOX_CMD_START(loadb)
 	.cmd = do_load_serial_bin,