1 | /* |
2 | * Copyright (c) 2008 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License |
8 | * Version 2.0 (the 'License'). You may not use this file except in |
9 | * compliance with the License. The rights granted to you under the License |
10 | * may not be used to create, or enable the creation or redistribution of, |
11 | * unlawful or unlicensed copies of an Apple operating system, or to |
12 | * circumvent, violate, or enable the circumvention or violation of, any |
13 | * terms of an Apple operating system software license agreement. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and |
24 | * limitations under the License. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | #include "kdp_serial.h" |
29 | #include <libkern/zlib.h> |
30 | #include <stdint.h> |
31 | #include <stdbool.h> |
32 | |
33 | #define SKDP_START_CHAR 0xFA |
34 | #define SKDP_END_CHAR 0xFB |
35 | #define SKDP_ESC_CHAR 0xFE |
36 | |
37 | static enum {DS_WAITSTART, DS_READING, DS_ESCAPED} dsState; |
38 | static unsigned char dsBuffer[1518]; |
39 | static int dsPos; |
40 | static uint32_t dsCRC; |
41 | static bool dsHaveCRC; |
42 | |
43 | |
44 | static void kdp_serial_out(unsigned char byte, void (*outFunc)(char)) |
45 | { |
46 | //need to escape '\n' because the kernel serial output turns it into a cr/lf |
47 | if(byte == SKDP_START_CHAR || byte == SKDP_END_CHAR || byte == SKDP_ESC_CHAR || byte == '\n') |
48 | { |
49 | outFunc(SKDP_ESC_CHAR); |
50 | byte = ~byte; |
51 | } |
52 | outFunc(byte); |
53 | } |
54 | |
55 | void kdp_serialize_packet(unsigned char *packet, unsigned int len, void (*outFunc)(char)) |
56 | { |
57 | unsigned int index; |
58 | unsigned char byte; |
59 | uint32_t crc; |
60 | |
61 | // insert the CRC between back to back STARTs which is compatible with old clients |
62 | crc = (uint32_t) z_crc32(0, packet, len); |
63 | outFunc(SKDP_START_CHAR); |
64 | kdp_serial_out((crc >> 0), outFunc); |
65 | kdp_serial_out((crc >> 8), outFunc); |
66 | kdp_serial_out((crc >> 16), outFunc); |
67 | kdp_serial_out((crc >> 24), outFunc); |
68 | |
69 | outFunc(SKDP_START_CHAR); |
70 | for (index = 0; index < len; index++) { |
71 | byte = *packet++; |
72 | kdp_serial_out(byte, outFunc); |
73 | } |
74 | outFunc(SKDP_END_CHAR); |
75 | } |
76 | |
77 | unsigned char *kdp_unserialize_packet(unsigned char byte, unsigned int *len) |
78 | { |
79 | uint32_t crc; |
80 | |
81 | switch(dsState) |
82 | { |
83 | case DS_WAITSTART: |
84 | if(byte == SKDP_START_CHAR) |
85 | { |
86 | // printf("got start char\n"); |
87 | dsState = DS_READING; |
88 | dsPos = 0; |
89 | *len = SERIALIZE_READING; |
90 | dsHaveCRC = false; |
91 | return 0; |
92 | } |
93 | *len = SERIALIZE_WAIT_START; |
94 | break; |
95 | case DS_READING: |
96 | if(byte == SKDP_ESC_CHAR) |
97 | { |
98 | dsState = DS_ESCAPED; |
99 | *len = SERIALIZE_READING; |
100 | return 0; |
101 | } |
102 | if(byte == SKDP_START_CHAR) |
103 | { |
104 | if (dsPos >= 4) |
105 | { |
106 | dsHaveCRC = true; |
107 | dsCRC = dsBuffer[0] | (dsBuffer[1] << 8) | (dsBuffer[2] << 16) | (dsBuffer[3] << 24); |
108 | } |
109 | //else printf("unexpected start char, resetting\n"); |
110 | dsPos = 0; |
111 | *len = SERIALIZE_READING; |
112 | return 0; |
113 | } |
114 | if(byte == SKDP_END_CHAR) |
115 | { |
116 | dsState = DS_WAITSTART; |
117 | if (dsHaveCRC) |
118 | { |
119 | crc = (uint32_t) z_crc32(0, &dsBuffer[0], dsPos); |
120 | if (crc != dsCRC) |
121 | { |
122 | // printf("bad packet crc 0x%x != 0x%x\n", crc, dsCRC); |
123 | dsPos = 0; |
124 | *len = SERIALIZE_WAIT_START; |
125 | return 0; |
126 | } |
127 | } |
128 | *len = dsPos; |
129 | dsPos = 0; |
130 | return dsBuffer; |
131 | } |
132 | dsBuffer[dsPos++] = byte; |
133 | break; |
134 | case DS_ESCAPED: |
135 | // printf("unescaping %02x to %02x\n", byte, ~byte); |
136 | dsBuffer[dsPos++] = ~byte; |
137 | dsState = DS_READING; |
138 | *len = SERIALIZE_READING; |
139 | break; |
140 | } |
141 | if(dsPos == sizeof(dsBuffer)) //too much data...forget this packet |
142 | { |
143 | dsState = DS_WAITSTART; |
144 | dsPos = 0; |
145 | *len = SERIALIZE_WAIT_START; |
146 | } |
147 | |
148 | return 0; |
149 | } |
150 | |