2017-07-31 13:26:29 +02:00
|
|
|
/*
|
2017-08-19 22:11:13 +02:00
|
|
|
* Copyright (C) 2017, Marcus Priesch, Ralf Schlatterbeck
|
2017-07-31 13:26:29 +02:00
|
|
|
* with code from the res-plugtest-large-update.c by
|
|
|
|
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Over-the-air update using blockwise transfer
|
|
|
|
* \author
|
|
|
|
* Marcus Priesch <marcus@priesch.co.at>
|
|
|
|
* Ralf Schlatterbeck <rsc@runtux.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "sys/cc.h"
|
|
|
|
#include "rest-engine.h"
|
|
|
|
#include "er-coap.h"
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "er-coap.h"
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include "bootloader_if.h"
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(x) printf x
|
|
|
|
#else
|
|
|
|
#define PRINTF(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// We allocate this statically, otherwise we cannot flash a new image
|
|
|
|
// when ram is exhausted!
|
|
|
|
static uint8_t current_page [256];
|
2017-08-20 00:12:57 +02:00
|
|
|
static uint32_t current_offset = 0;
|
2017-07-31 13:26:29 +02:00
|
|
|
#define PAGESIZE (sizeof (current_page))
|
|
|
|
|
2017-08-27 15:00:04 +02:00
|
|
|
/*
|
|
|
|
* Note that the current code relies on the fact that the bootloader
|
|
|
|
* used only supports two images. This may change in the future. We
|
|
|
|
* mainly need to relax some of the checks and use a different algorithm
|
|
|
|
* for computing imgidx, the index of the partition to be overwritten.
|
|
|
|
* If the bootloader supports more than two partitions at some point we
|
|
|
|
* may want the uploader to explicitly define the partition to be used.
|
|
|
|
*/
|
|
|
|
|
2017-07-31 13:26:29 +02:00
|
|
|
static void
|
2017-08-27 15:00:04 +02:00
|
|
|
res_put_handler
|
|
|
|
( void *request
|
|
|
|
, void *response
|
|
|
|
, uint8_t *buffer
|
|
|
|
, uint16_t preferred_size
|
|
|
|
, int32_t *offset
|
|
|
|
)
|
2017-07-31 13:26:29 +02:00
|
|
|
{
|
|
|
|
coap_packet_t *const packet = (coap_packet_t *)request;
|
|
|
|
uint8_t *in_data = NULL;
|
|
|
|
size_t len = 0;
|
2017-08-24 13:55:21 +02:00
|
|
|
uint32_t partition_start = 0;
|
2017-08-27 15:00:04 +02:00
|
|
|
const uint32_t partition_size = bootloader_get_part_size ();
|
|
|
|
uint32_t imgidx = 0;
|
2017-07-31 13:26:29 +02:00
|
|
|
unsigned int ct = -1;
|
|
|
|
|
2017-08-27 15:00:04 +02:00
|
|
|
/* If the currently-booted partition is not the default partition we
|
|
|
|
* do not allow overwriting a partition: Neither the currently-booted
|
|
|
|
* one (this would crash) nor the only partition that is marked
|
|
|
|
* bootable. We also insist that boot_next == boot_default.
|
|
|
|
*/
|
|
|
|
if (bootloader_get_boot_default () != bootloader_get_boot_next ()) {
|
|
|
|
REST.set_response_status (response, REST.status.BAD_REQUEST);
|
|
|
|
const char *error_msg = "Won't overwrite boot_next";
|
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bootloader_get_boot_default () != bootloader_get_active_part ()) {
|
|
|
|
REST.set_response_status (response, REST.status.BAD_REQUEST);
|
|
|
|
const char *error_msg = "Won't overwrite current";
|
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
|
|
|
return;
|
2017-08-24 13:55:21 +02:00
|
|
|
}
|
2017-08-27 15:00:04 +02:00
|
|
|
imgidx = !bootloader_get_active_part ();
|
|
|
|
partition_start = bootloader_get_part_start (imgidx);
|
2017-08-24 13:55:21 +02:00
|
|
|
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.get_header_content_type (request, &ct);
|
2017-07-31 13:26:29 +02:00
|
|
|
|
|
|
|
/* Require content_type APPLICATION_OCTET_STREAM */
|
|
|
|
if (ct != REST.type.APPLICATION_OCTET_STREAM) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.BAD_REQUEST);
|
2017-07-31 13:26:29 +02:00
|
|
|
const char *error_msg = "ContentType";
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = REST.get_request_payload (request, (const uint8_t **)&in_data);
|
|
|
|
PRINTF (("cur: %lu len: %lu, offset: %lu\n",
|
|
|
|
(uint32_t)current_offset, (uint32_t)len, (uint32_t)*offset));
|
|
|
|
PRINTF (("b1-offs: %lu, b1-size: %u, b1-num: %lu b1-more: %d b1-size1: %lu\n",
|
|
|
|
packet->block1_offset, packet->block1_size, packet->block1_num,
|
|
|
|
packet->block1_more, packet->size1));
|
|
|
|
if (len == 0 || NULL == in_data) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.BAD_REQUEST);
|
2017-07-31 13:26:29 +02:00
|
|
|
const char *error_msg = "NoPayload";
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-20 16:43:11 +02:00
|
|
|
/* if the block1_offset is 0 a new transmission has started */
|
|
|
|
if (!packet->block1_offset) {
|
|
|
|
current_offset = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-31 13:26:29 +02:00
|
|
|
if (packet->block1_offset > current_offset) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.REQUEST_ENTITY_INCOMPLETE);
|
2017-07-31 13:26:29 +02:00
|
|
|
const char *error_msg = "OutOfSequence";
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Old packet or retransmission, immediately confirm */
|
2017-08-20 16:43:11 +02:00
|
|
|
if (packet->block1_offset && packet->block1_offset + len <= current_offset) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.CHANGED);
|
2017-07-31 13:26:29 +02:00
|
|
|
coap_set_header_block1
|
|
|
|
(response, packet->block1_num, 0, packet->block1_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: blocksize may be larger than our flash page size
|
|
|
|
if (len > PAGESIZE) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.INTERNAL_SERVER_ERROR);
|
2017-07-31 13:26:29 +02:00
|
|
|
const char *error_msg = "GRMPF: PageSize";
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FIXME: blocksize may be larger than our flash page size
|
|
|
|
// So we should handle this case and repeatedly flash a block until the
|
|
|
|
// received data is written.
|
|
|
|
if (current_offset % PAGESIZE + len > PAGESIZE) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.INTERNAL_SERVER_ERROR);
|
2017-07-31 13:26:29 +02:00
|
|
|
const char *error_msg = "GRMPF: blocksize";
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Should never happen, we test for < and > earlier.
|
|
|
|
if (packet->block1_offset != current_offset) {
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.INTERNAL_SERVER_ERROR);
|
2017-07-31 13:26:29 +02:00
|
|
|
const char *error_msg = "GRMPF: Offset";
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_payload (response, error_msg, strlen (error_msg));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-27 15:00:04 +02:00
|
|
|
if (packet->block1_offset + len > partition_size) {
|
|
|
|
REST.set_response_status
|
|
|
|
(response, REST.status.REQUEST_ENTITY_TOO_LARGE);
|
|
|
|
REST.set_response_payload
|
|
|
|
(response, buffer, sprintf ((char *)buffer, "%luB max.", partition_size));
|
2017-07-31 13:26:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (current_page + current_offset % PAGESIZE, in_data, len);
|
2017-08-27 15:00:04 +02:00
|
|
|
/* Whenever an upload is started for a partition mark it as not ok */
|
|
|
|
if (current_offset == 0) {
|
|
|
|
PRINTF (("Clear partition_ok: %ld\n", imgidx));
|
|
|
|
bootloader_clr_part_ok (imgidx);
|
|
|
|
}
|
2017-08-19 22:11:13 +02:00
|
|
|
current_offset += len;
|
|
|
|
|
|
|
|
if (current_offset % PAGESIZE == 0) {
|
|
|
|
uint32_t dst_address = partition_start + current_offset - PAGESIZE;
|
2017-08-27 15:00:04 +02:00
|
|
|
/* Special case: Flash irq vectors to backup position */
|
|
|
|
if (current_offset - PAGESIZE < PART_IRQVEC_SIZE) {
|
|
|
|
/* Only for images not at position 0 write first PART_IRQVEC_SIZE
|
|
|
|
* bytes also to original position. For partition 0 it will be
|
|
|
|
* copied there anyway *and* we would crash if we wrote to the
|
|
|
|
* active memory!
|
|
|
|
*/
|
|
|
|
if (partition_start != 0) {
|
|
|
|
PRINTF (("Flashing: %lx to %lx\n", (uint32_t)PAGESIZE, dst_address));
|
|
|
|
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
|
|
|
|
}
|
|
|
|
/* Note: The partition_size returned by the bootloader does *NOT*
|
|
|
|
* include the PART_IRQVEC_SIZE
|
|
|
|
*/
|
|
|
|
dst_address = partition_start + partition_size
|
|
|
|
+ current_offset - PAGESIZE;
|
|
|
|
}
|
|
|
|
PRINTF (("Flashing: %lx to %lx\n", (uint32_t)PAGESIZE, dst_address));
|
2017-08-19 22:11:13 +02:00
|
|
|
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
|
|
|
|
} else if (!packet->block1_more) {
|
|
|
|
uint32_t dst_address =
|
|
|
|
partition_start + (current_offset / PAGESIZE) * PAGESIZE;
|
2017-08-27 15:00:04 +02:00
|
|
|
PRINTF (("Flashing: last %lx to %lx\n", (uint32_t)PAGESIZE, dst_address));
|
2017-08-19 22:11:13 +02:00
|
|
|
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
|
2017-07-31 13:26:29 +02:00
|
|
|
}
|
2017-08-19 22:11:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (!packet->block1_more) {
|
|
|
|
// we are finished
|
2017-08-27 15:00:04 +02:00
|
|
|
bootloader_set_boot_next (imgidx);
|
2017-08-20 16:43:11 +02:00
|
|
|
current_offset = 0;
|
2017-08-19 22:11:13 +02:00
|
|
|
}
|
2017-07-31 13:26:29 +02:00
|
|
|
|
2017-08-27 15:00:04 +02:00
|
|
|
REST.set_response_status (response, REST.status.CHANGED);
|
|
|
|
coap_set_header_block1 (response, packet->block1_num, 0, packet->block1_size);
|
2017-07-31 13:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RESOURCE(
|
|
|
|
res_upload_image
|
|
|
|
, "title=\"Flash memory upgrade\";rt=\"block\""
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, res_put_handler
|
|
|
|
, NULL
|
|
|
|
);
|
|
|
|
|