1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "boot_test.h"
21 
TEST_CASE(boot_serial_img_msg)22 TEST_CASE(boot_serial_img_msg)
23 {
24     char img[16];
25     char enc_img[BASE64_ENCODE_SIZE(sizeof(img)) + 1];
26     char buf[sizeof(struct nmgr_hdr) + sizeof(enc_img) + 32];
27     int len;
28     int rc;
29     struct nmgr_hdr *hdr;
30     const struct flash_area *fap;
31 
32     /* 00000000  a3 64 64 61 74 61 58 10  |.ddataX.|
33      * 00000008  a5 a5 a5 a5 a5 a5 a5 a5  |........|
34      * 00000010  a5 a5 a5 a5 a5 a5 a5 a5  |........|
35      * 00000018  63 6c 65 6e 1a 00 01 14  |clen....|
36      * 00000020  e8 63 6f 66 66 00        |.coff.|
37      */
38     static const uint8_t payload[] = {
39         0xa3, 0x64, 0x64, 0x61, 0x74, 0x61, 0x58, 0x10,
40         /* 16 bytes of image data starts here. */
41         0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
42         0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
43         0x63, 0x6c, 0x65, 0x6e, 0x1a, 0x00, 0x01, 0x14,
44         0xe8, 0x63, 0x6f, 0x66, 0x66, 0x00,
45     };
46 
47     memset(img, 0xa5, sizeof(img));
48 
49     hdr = (struct nmgr_hdr *)buf;
50     memset(hdr, 0, sizeof(*hdr));
51     hdr->nh_op = NMGR_OP_WRITE;
52     hdr->nh_group = htons(MGMT_GROUP_ID_IMAGE);
53     hdr->nh_id = IMGMGR_NMGR_ID_UPLOAD;
54 
55     memcpy(hdr + 1, payload, sizeof payload);
56     hdr->nh_len = htons(sizeof payload);
57 
58     len = sizeof(*hdr) + sizeof payload;
59     tx_msg(buf, len);
60 
61     /*
62      * Validate contents inside the primary slot
63      */
64     rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
65     assert(rc == 0);
66 
67     rc = flash_area_read(fap, 0, enc_img, sizeof(img));
68     assert(rc == 0);
69     assert(!memcmp(enc_img, img, sizeof(img)));
70 }
71