1 // SPDX-License-Identifier: GPL-2.0+
8 #include <asm/unaligned.h>
9 #include <linux/compiler.h>
10 #include <linux/device.h>
11 #include <linux/types.h>
13 #include <linux/surface_aggregator/serial_hub.h>
14 #include "ssh_parser.h"
17 * sshp_validate_crc() - Validate a CRC in raw message data.
18 * @src: The span of data over which the CRC should be computed.
19 * @crc: The pointer to the expected u16 CRC value.
21 * Computes the CRC of the provided data span (@src), compares it to the CRC
22 * stored at the given address (@crc), and returns the result of this
23 * comparison, i.e. %true if equal. This function is intended to run on raw
26 * Return: Returns %true if the computed CRC matches the stored CRC, %false
29 static bool sshp_validate_crc(const struct ssam_span *src, const u8 *crc)
31 u16 actual = ssh_crc(src->ptr, src->len);
32 u16 expected = get_unaligned_le16(crc);
34 return actual == expected;
38 * sshp_starts_with_syn() - Check if the given data starts with SSH SYN bytes.
39 * @src: The data span to check the start of.
41 static bool sshp_starts_with_syn(const struct ssam_span *src)
43 return src->len >= 2 && get_unaligned_le16(src->ptr) == SSH_MSG_SYN;
47 * sshp_find_syn() - Find SSH SYN bytes in the given data span.
48 * @src: The data span to search in.
49 * @rem: The span (output) indicating the remaining data, starting with SSH
50 * SYN bytes, if found.
52 * Search for SSH SYN bytes in the given source span. If found, set the @rem
53 * span to the remaining data, starting with the first SYN bytes and capped by
54 * the source span length, and return %true. This function does not copy any
55 * data, but rather only sets pointers to the respective start addresses and
58 * If no SSH SYN bytes could be found, set the @rem span to the zero-length
59 * span at the end of the source span and return %false.
61 * If partial SSH SYN bytes could be found at the end of the source span, set
62 * the @rem span to cover these partial SYN bytes, capped by the end of the
63 * source span, and return %false. This function should then be re-run once
64 * more data is available.
66 * Return: Returns %true if a complete SSH SYN sequence could be found,
69 bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem)
73 for (i = 0; i < src->len - 1; i++) {
74 if (likely(get_unaligned_le16(src->ptr + i) == SSH_MSG_SYN)) {
75 rem->ptr = src->ptr + i;
76 rem->len = src->len - i;
81 if (unlikely(src->ptr[src->len - 1] == (SSH_MSG_SYN & 0xff))) {
82 rem->ptr = src->ptr + src->len - 1;
87 rem->ptr = src->ptr + src->len;
93 * sshp_parse_frame() - Parse SSH frame.
94 * @dev: The device used for logging.
95 * @source: The source to parse from.
96 * @frame: The parsed frame (output).
97 * @payload: The parsed payload (output).
98 * @maxlen: The maximum supported message length.
100 * Parses and validates a SSH frame, including its payload, from the given
101 * source. Sets the provided @frame pointer to the start of the frame and
102 * writes the limits of the frame payload to the provided @payload span
105 * This function does not copy any data, but rather only validates the message
106 * data and sets pointers (and length values) to indicate the respective parts.
108 * If no complete SSH frame could be found, the frame pointer will be set to
109 * the %NULL pointer and the payload span will be set to the null span (start
110 * pointer %NULL, size zero).
112 * Return: Returns zero on success or if the frame is incomplete, %-ENOMSG if
113 * the start of the message is invalid, %-EBADMSG if any (frame-header or
114 * payload) CRC is invalid, or %-EMSGSIZE if the SSH message is bigger than
115 * the maximum message length specified in the @maxlen parameter.
117 int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
118 struct ssh_frame **frame, struct ssam_span *payload,
124 /* Initialize output. */
129 if (!sshp_starts_with_syn(source)) {
130 dev_warn(dev, "rx: parser: invalid start of frame\n");
134 /* Check for minimum packet length. */
135 if (unlikely(source->len < SSH_MESSAGE_LENGTH(0))) {
136 dev_dbg(dev, "rx: parser: not enough data for frame\n");
140 /* Pin down frame. */
141 sf.ptr = source->ptr + sizeof(u16);
142 sf.len = sizeof(struct ssh_frame);
144 /* Validate frame CRC. */
145 if (unlikely(!sshp_validate_crc(&sf, sf.ptr + sf.len))) {
146 dev_warn(dev, "rx: parser: invalid frame CRC\n");
150 /* Ensure packet does not exceed maximum length. */
151 sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
152 if (unlikely(SSH_MESSAGE_LENGTH(sp.len) > maxlen)) {
153 dev_warn(dev, "rx: parser: frame too large: %llu bytes\n",
154 SSH_MESSAGE_LENGTH(sp.len));
158 /* Pin down payload. */
159 sp.ptr = sf.ptr + sf.len + sizeof(u16);
161 /* Check for frame + payload length. */
162 if (source->len < SSH_MESSAGE_LENGTH(sp.len)) {
163 dev_dbg(dev, "rx: parser: not enough data for payload\n");
167 /* Validate payload CRC. */
168 if (unlikely(!sshp_validate_crc(&sp, sp.ptr + sp.len))) {
169 dev_warn(dev, "rx: parser: invalid payload CRC\n");
173 *frame = (struct ssh_frame *)sf.ptr;
176 dev_dbg(dev, "rx: parser: valid frame found (type: %#04x, len: %u)\n",
177 (*frame)->type, (*frame)->len);
183 * sshp_parse_command() - Parse SSH command frame payload.
184 * @dev: The device used for logging.
185 * @source: The source to parse from.
186 * @command: The parsed command (output).
187 * @command_data: The parsed command data/payload (output).
189 * Parses and validates a SSH command frame payload. Sets the @command pointer
190 * to the command header and the @command_data span to the command data (i.e.
191 * payload of the command). This will result in a zero-length span if the
192 * command does not have any associated data/payload. This function does not
193 * check the frame-payload-type field, which should be checked by the caller
194 * before calling this function.
196 * The @source parameter should be the complete frame payload, e.g. returned
197 * by the sshp_parse_frame() command.
199 * This function does not copy any data, but rather only validates the frame
200 * payload data and sets pointers (and length values) to indicate the
203 * Return: Returns zero on success or %-ENOMSG if @source does not represent a
204 * valid command-type frame payload, i.e. is too short.
206 int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
207 struct ssh_command **command,
208 struct ssam_span *command_data)
210 /* Check for minimum length. */
211 if (unlikely(source->len < sizeof(struct ssh_command))) {
213 command_data->ptr = NULL;
214 command_data->len = 0;
216 dev_err(dev, "rx: parser: command payload is too short\n");
220 *command = (struct ssh_command *)source->ptr;
221 command_data->ptr = source->ptr + sizeof(struct ssh_command);
222 command_data->len = source->len - sizeof(struct ssh_command);
224 dev_dbg(dev, "rx: parser: valid command found (tc: %#04x, cid: %#04x)\n",
225 (*command)->tc, (*command)->cid);