aboutsummaryrefslogtreecommitdiffstats
path: root/src/download.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/download.c')
-rw-r--r--src/download.c224
1 files changed, 224 insertions, 0 deletions
diff --git a/src/download.c b/src/download.c
new file mode 100644
index 0000000..741e2e5
--- /dev/null
+++ b/src/download.c
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <endian.h>
+
+#include "lg-downloader.h"
+#include "hdlc.h"
+#include "usb.h"
+#include "download.h"
+
+static int download_response_check(struct download_response *response)
+{
+ if (response == NULL)
+ return -1;
+
+ if (response->ack != DOWNLOAD_ACK)
+ return -1;
+
+ return 0;
+}
+
+static int download_response(struct context *context)
+{
+ struct download_response response;
+ int rc;
+
+ rc = hdlc_recv(context, &response, sizeof(response));
+ if (rc < 0)
+ return -1;
+
+ rc = download_response_check(&response);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+static int download_request(struct context *context, unsigned char command)
+{
+ struct download_request request;
+ int rc;
+
+ memset(&request, 0, sizeof(request));
+ request.command = command;
+
+ rc = hdlc_send(context, &request, sizeof(request));
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_start_flash_write(struct context *context)
+{
+ int rc;
+
+ rc = download_request(context, DOWNLOAD_START_FLASH_WRITE);
+ if (rc < 0)
+ return -1;
+
+ rc = download_response(context);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_write(struct context *context, void *buffer, unsigned int address, unsigned int length)
+{
+ struct download_write_request request;
+ struct download_response response;
+ int rc;
+
+ memset(&request, 0, sizeof(request));
+ request.command = DOWNLOAD_WRITE;
+ request.address = htole32(address);
+ request.length = htole32(length);
+
+ rc = hdlc_send(context, &request, sizeof(request));
+ if (rc < 0)
+ return -1;
+
+ rc = usb_send(context, buffer, length);
+ if (rc < 0)
+ return -1;
+
+ rc = hdlc_recv(context, &response, sizeof(response));
+ if (rc < 0)
+ return -1;
+
+ if (response.ack != DOWNLOAD_WRITE)
+ return -1;
+
+ return 0;
+}
+
+int download_erase(struct context *context, unsigned int address, unsigned int length)
+{
+ struct download_erase_request request;
+ int rc;
+
+ memset(&request, 0, sizeof(request));
+ request.command = DOWNLOAD_ERASE;
+ request.address = htole32(address);
+ request.length = htole32(length);
+
+ rc = hdlc_send(context, &request, sizeof(request));
+ if (rc < 0)
+ return -1;
+
+ rc = download_response(context);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_read(struct context *context, void *buffer, unsigned int length)
+{
+ struct download_read_request request;
+ int rc;
+
+ memset(&request, 0, sizeof(request));
+ request.command = DOWNLOAD_READ;
+ request.length = htole32(length);
+
+ rc = hdlc_send(context, &request, sizeof(request));
+ if (rc < 0)
+ return -1;
+
+ rc = usb_recv(context, buffer, length);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_initialize_partition(struct context *context, unsigned int address, unsigned int length)
+{
+ struct download_initialize_partition_request request;
+ int rc;
+
+ memset(&request, 0, sizeof(request));
+ request.command = DOWNLOAD_INITIALIZE_PARTITION;
+ request.address = htole32(address);
+ request.length = htole32(length);
+
+ rc = hdlc_send(context, &request, sizeof(request));
+ if (rc < 0)
+ return -1;
+
+ rc = download_response(context);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_ready_read(struct context *context, unsigned int address, unsigned int length)
+{
+ struct download_ready_read_request request;
+ int rc;
+
+ memset(&request, 0, sizeof(request));
+ request.command = DOWNLOAD_READY_READ;
+ request.address = htole32(address);
+ request.length = htole32(length);
+
+ rc = hdlc_send(context, &request, sizeof(request));
+ if (rc < 0)
+ return -1;
+
+ rc = download_response(context);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_reset(struct context *context)
+{
+ int rc;
+
+ rc = download_request(context, DOWNLOAD_RESET);
+ if (rc < 0)
+ return -1;
+
+ rc = download_response(context);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}
+
+int download_notify_start_dl(struct context *context)
+{
+ int rc;
+
+ rc = download_request(context, DOWNLOAD_NOTIFY_START_DL);
+ if (rc < 0)
+ return -1;
+
+ rc = download_response(context);
+ if (rc < 0)
+ return -1;
+
+ return 0;
+}