/* * Copyright (C) 2016 Paul Kocialkowski * * 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 . */ #include #include #include #include #include #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, uint8_t 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, off_t address, size_t 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, off_t address, size_t 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, size_t 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, off_t address, size_t 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, off_t address, size_t 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; }