]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/kdp_serial.c
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include "kdp_serial.h"
30 #define SKDP_START_CHAR 0xFA
31 #define SKDP_END_CHAR 0xFB
32 #define SKDP_ESC_CHAR 0xFE
34 static enum {DS_WAITSTART
, DS_READING
, DS_ESCAPED
} dsState
;
35 static unsigned char dsBuffer
[1518];
38 void kdp_serialize_packet(unsigned char *packet
, unsigned int len
, void (*outFunc
)(char))
41 outFunc(SKDP_START_CHAR
);
42 for (index
= 0; index
< len
; index
++) {
43 unsigned char byte
= *packet
++;
44 //need to escape '\n' because the kernel serial output turns it into a cr/lf
45 if(byte
== SKDP_START_CHAR
|| byte
== SKDP_END_CHAR
|| byte
== SKDP_ESC_CHAR
|| byte
== '\n')
47 outFunc(SKDP_ESC_CHAR
);
52 outFunc(SKDP_END_CHAR
);
55 unsigned char *kdp_unserialize_packet(unsigned char byte
, unsigned int *len
)
60 if(byte
== SKDP_START_CHAR
)
62 // printf("got start char\n");
65 *len
= SERIALIZE_READING
;
68 *len
= SERIALIZE_WAIT_START
;
71 if(byte
== SKDP_ESC_CHAR
)
74 *len
= SERIALIZE_READING
;
77 if(byte
== SKDP_START_CHAR
)
79 // printf("unexpected start char, resetting\n");
81 *len
= SERIALIZE_READING
;
84 if(byte
== SKDP_END_CHAR
)
86 dsState
= DS_WAITSTART
;
91 dsBuffer
[dsPos
++] = byte
;
94 // printf("unescaping %02x to %02x\n", byte, ~byte);
95 dsBuffer
[dsPos
++] = ~byte
;
97 *len
= SERIALIZE_READING
;
100 if(dsPos
== sizeof(dsBuffer
)) //too much data...forget this packet
102 dsState
= DS_WAITSTART
;
104 *len
= SERIALIZE_WAIT_START
;