1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "console.h"
33 #include "fcntl.h"
34 #include "sys/ioctl.h"
35 #ifdef LOSCFG_FILE_MODE
36 #include "stdarg.h"
37 #endif
38 #include "unistd.h"
39 #include "securec.h"
40 #ifdef LOSCFG_SHELL_DMESG
41 #include "dmesg_pri.h"
42 #endif
43 #ifdef LOSCFG_SHELL
44 #include "shcmd.h"
45 #include "shell_pri.h"
46 #endif
47 #include "los_exc_pri.h"
48 #include "los_process_pri.h"
49 #include "los_sched_pri.h"
50 #include "user_copy.h"
51 #include "fs/driver.h"
52
53 #define EACH_CHAR 1
54 #define UART_IOC_MAGIC 'u'
55 #define UART_CFG_ATTR _IOW(UART_IOC_MAGIC, 5, int)
56 #define UART_CFG_PRIVATE _IOW(UART_IOC_MAGIC, 6, int)
57 /* Inter-module variable */
58 extern UINT32 g_uart_fputc_en;
59 STATIC UINT32 ConsoleSendTask(UINTPTR param);
60
61 STATIC UINT8 g_taskConsoleIDArray[LOSCFG_BASE_CORE_TSK_LIMIT];
62 STATIC SPIN_LOCK_INIT(g_consoleSpin);
63 STATIC SPIN_LOCK_INIT(g_consoleWriteSpinLock);
64
65 #define SHELL_ENTRYID_INVALID 0xFFFFFFFF
66 #define SHELL_TASK_PRIORITY 9
67 #define CONSOLE_CIRBUF_EVENT 0x02U
68 #define CONSOLE_SEND_TASK_EXIT 0x04U
69 #define CONSOLE_SEND_TASK_RUNNING 0x10U
70
71 #define SHELL_ENTRY_NAME "ShellEntry"
72 #define SHELL_ENTRY_NAME_LEN 10
73
74 CONSOLE_CB *g_console[CONSOLE_NUM];
75 #define MIN(a, b) ((a) < (b) ? (a) : (b))
76
77 /*
78 * acquire uart driver function and filep of /dev/console,
79 * then store uart driver function in *filepOps
80 * and store filep of /dev/console in *privFilep.
81 */
GetFilepOps(const struct file * filep,struct file ** privFilep,const struct file_operations_vfs ** filepOps)82 INT32 GetFilepOps(const struct file *filep, struct file **privFilep, const struct file_operations_vfs **filepOps)
83 {
84 INT32 ret;
85
86 if ((filep == NULL) || (filep->f_vnode == NULL) || (filep->f_vnode->data == NULL)) {
87 ret = EINVAL;
88 goto ERROUT;
89 }
90
91 /* to find console device's filep(now it is *privFilep) through i_private */
92 struct drv_data *drv = (struct drv_data *)filep->f_vnode->data;
93 *privFilep = (struct file *)drv->priv;
94 if (((*privFilep)->f_vnode == NULL) || ((*privFilep)->f_vnode->data == NULL)) {
95 ret = EINVAL;
96 goto ERROUT;
97 }
98
99 /* to find uart driver operation function through u.i_opss */
100
101 drv = (struct drv_data *)(*privFilep)->f_vnode->data;
102
103 *filepOps = (const struct file_operations_vfs *)drv->ops;
104
105 return ENOERR;
106 ERROUT:
107 set_errno(ret);
108 return VFS_ERROR;
109 }
110
ConsoleTcGetAttr(INT32 fd,struct termios * termios)111 INT32 ConsoleTcGetAttr(INT32 fd, struct termios *termios)
112 {
113 struct file *filep = NULL;
114 CONSOLE_CB *consoleCB = NULL;
115
116 INT32 ret = fs_getfilep(fd, &filep);
117 if (ret < 0) {
118 return -EPERM;
119 }
120
121 consoleCB = (CONSOLE_CB *)filep->f_priv;
122 if (consoleCB == NULL) {
123 return -EFAULT;
124 }
125
126 (VOID)memcpy_s(termios, sizeof(struct termios), &consoleCB->consoleTermios, sizeof(struct termios));
127 return LOS_OK;
128 }
129
ConsoleTcSetAttr(INT32 fd,INT32 actions,const struct termios * termios)130 INT32 ConsoleTcSetAttr(INT32 fd, INT32 actions, const struct termios *termios)
131 {
132 struct file *filep = NULL;
133 CONSOLE_CB *consoleCB = NULL;
134
135 (VOID)actions;
136
137 INT32 ret = fs_getfilep(fd, &filep);
138 if (ret < 0) {
139 return -EPERM;
140 }
141
142 consoleCB = (CONSOLE_CB *)filep->f_priv;
143 if (consoleCB == NULL) {
144 return -EFAULT;
145 }
146
147 (VOID)memcpy_s(&consoleCB->consoleTermios, sizeof(struct termios), termios, sizeof(struct termios));
148 return LOS_OK;
149 }
150
ConsoleRefcountGet(const CONSOLE_CB * consoleCB)151 STATIC UINT32 ConsoleRefcountGet(const CONSOLE_CB *consoleCB)
152 {
153 return consoleCB->refCount;
154 }
155
ConsoleRefcountSet(CONSOLE_CB * consoleCB,BOOL flag)156 STATIC VOID ConsoleRefcountSet(CONSOLE_CB *consoleCB, BOOL flag)
157 {
158 (consoleCB->refCount) += flag ? 1 : -1;
159 }
160
IsConsoleOccupied(const CONSOLE_CB * consoleCB)161 BOOL IsConsoleOccupied(const CONSOLE_CB *consoleCB)
162 {
163 return ConsoleRefcountGet(consoleCB);
164 }
165
ConsoleCtrlCaptureLine(CONSOLE_CB * consoleCB)166 STATIC INT32 ConsoleCtrlCaptureLine(CONSOLE_CB *consoleCB)
167 {
168 struct termios consoleTermios = {0};
169 UINT32 intSave;
170
171 LOS_SpinLockSave(&g_consoleSpin, &intSave);
172 (VOID)ConsoleTcGetAttr(consoleCB->fd, &consoleTermios);
173 consoleTermios.c_lflag |= ICANON | ECHO;
174 (VOID)ConsoleTcSetAttr(consoleCB->fd, 0, &consoleTermios);
175 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
176
177 return LOS_OK;
178 }
179
ConsoleCtrlCaptureChar(CONSOLE_CB * consoleCB)180 STATIC INT32 ConsoleCtrlCaptureChar(CONSOLE_CB *consoleCB)
181 {
182 struct termios consoleTermios = {0};
183 UINT32 intSave;
184
185 LOS_SpinLockSave(&g_consoleSpin, &intSave);
186 (VOID)ConsoleTcGetAttr(consoleCB->fd, &consoleTermios);
187 consoleTermios.c_lflag &= ~(ICANON | ECHO);
188 (VOID)ConsoleTcSetAttr(consoleCB->fd, 0, &consoleTermios);
189 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
190
191 return LOS_OK;
192 }
193
ConsoleCtrlRightsCapture(CONSOLE_CB * consoleCB)194 STATIC INT32 ConsoleCtrlRightsCapture(CONSOLE_CB *consoleCB)
195 {
196 (VOID)LOS_SemPend(consoleCB->consoleSem, LOS_WAIT_FOREVER);
197 if ((ConsoleRefcountGet(consoleCB) == 0) &&
198 (OsCurrTaskGet()->taskID != consoleCB->shellEntryId)) {
199 /* not 0:indicate that shellentry is in uart_read, suspend shellentry task directly */
200 (VOID)LOS_TaskSuspend(consoleCB->shellEntryId);
201 }
202 ConsoleRefcountSet(consoleCB, TRUE);
203 return LOS_OK;
204 }
205
ConsoleCtrlRightsRelease(CONSOLE_CB * consoleCB)206 STATIC INT32 ConsoleCtrlRightsRelease(CONSOLE_CB *consoleCB)
207 {
208 if (ConsoleRefcountGet(consoleCB) == 0) {
209 PRINT_ERR("console is free\n");
210 (VOID)LOS_SemPost(consoleCB->consoleSem);
211 return LOS_NOK;
212 } else {
213 ConsoleRefcountSet(consoleCB, FALSE);
214 if ((ConsoleRefcountGet(consoleCB) == 0) &&
215 (OsCurrTaskGet()->taskID != consoleCB->shellEntryId)) {
216 (VOID)LOS_TaskResume(consoleCB->shellEntryId);
217 }
218 }
219 (VOID)LOS_SemPost(consoleCB->consoleSem);
220 return LOS_OK;
221 }
222
OsGetConsoleByDevice(const CHAR * deviceName)223 STATIC CONSOLE_CB *OsGetConsoleByDevice(const CHAR *deviceName)
224 {
225 INT32 ret;
226 struct Vnode *vnode = NULL;
227
228 VnodeHold();
229 ret = VnodeLookup(deviceName, &vnode, 0);
230 VnodeDrop();
231 if (ret < 0) {
232 set_errno(EACCES);
233 return NULL;
234 }
235
236 if (g_console[CONSOLE_SERIAL - 1]->devVnode == vnode) {
237 return g_console[CONSOLE_SERIAL - 1];
238 } else if (g_console[CONSOLE_TELNET - 1]->devVnode == vnode) {
239 return g_console[CONSOLE_TELNET - 1];
240 } else {
241 set_errno(ENOENT);
242 return NULL;
243 }
244 }
245
OsGetConsoleID(const CHAR * deviceName)246 STATIC INT32 OsGetConsoleID(const CHAR *deviceName)
247 {
248 if ((deviceName != NULL) &&
249 (strlen(deviceName) == strlen(SERIAL)) &&
250 (!strncmp(deviceName, SERIAL, strlen(SERIAL)))) {
251 return CONSOLE_SERIAL;
252 }
253 #ifdef LOSCFG_NET_TELNET
254 else if ((deviceName != NULL) &&
255 (strlen(deviceName) == strlen(TELNET)) &&
256 (!strncmp(deviceName, TELNET, strlen(TELNET)))) {
257 return CONSOLE_TELNET;
258 }
259 #endif
260 return -1;
261 }
262
OsConsoleFullpathToID(const CHAR * fullpath)263 STATIC INT32 OsConsoleFullpathToID(const CHAR *fullpath)
264 {
265 #define CONSOLE_SERIAL_1 "/dev/console1"
266 #define CONSOLE_TELNET_2 "/dev/console2"
267
268 size_t len;
269
270 if (fullpath == NULL) {
271 return -1;
272 }
273
274 len = strlen(fullpath);
275 if ((len == strlen(CONSOLE_SERIAL_1)) &&
276 (!strncmp(fullpath, CONSOLE_SERIAL_1, strlen(CONSOLE_SERIAL_1)))) {
277 return CONSOLE_SERIAL;
278 }
279 #ifdef LOSCFG_NET_TELNET
280 else if ((len == strlen(CONSOLE_TELNET_2)) &&
281 (!strncmp(fullpath, CONSOLE_TELNET_2, strlen(CONSOLE_TELNET_2)))) {
282 return CONSOLE_TELNET;
283 }
284 #endif
285 return -1;
286 }
287
ConsoleFifoEmpty(const CONSOLE_CB * console)288 STATIC BOOL ConsoleFifoEmpty(const CONSOLE_CB *console)
289 {
290 return console->fifoOut == console->fifoIn;
291 }
292
ConsoleFifoClearup(CONSOLE_CB * console)293 STATIC VOID ConsoleFifoClearup(CONSOLE_CB *console)
294 {
295 console->fifoOut = 0;
296 console->fifoIn = 0;
297 (VOID)memset_s(console->fifo, CONSOLE_FIFO_SIZE, 0, CONSOLE_FIFO_SIZE);
298 }
299
ConsoleFifoLenUpdate(CONSOLE_CB * console)300 STATIC VOID ConsoleFifoLenUpdate(CONSOLE_CB *console)
301 {
302 console->currentLen = console->fifoIn - console->fifoOut;
303 }
304
ConsoleReadFifo(CHAR * buffer,CONSOLE_CB * console,size_t bufLen)305 STATIC INT32 ConsoleReadFifo(CHAR *buffer, CONSOLE_CB *console, size_t bufLen)
306 {
307 INT32 ret;
308 UINT32 readNum;
309
310 readNum = MIN(bufLen, console->currentLen);
311 ret = memcpy_s(buffer, bufLen, console->fifo + console->fifoOut, readNum);
312 if (ret != EOK) {
313 PRINTK("%s,%d memcpy_s failed\n", __FUNCTION__, __LINE__);
314 return -1;
315 }
316 console->fifoOut += readNum;
317 if (ConsoleFifoEmpty(console)) {
318 ConsoleFifoClearup(console);
319 }
320 ConsoleFifoLenUpdate(console);
321 return (INT32)readNum;
322 }
323
FilepOpen(struct file * filep,const struct file_operations_vfs * fops)324 INT32 FilepOpen(struct file *filep, const struct file_operations_vfs *fops)
325 {
326 INT32 ret;
327 if (fops->open == NULL) {
328 return -EFAULT;
329 }
330
331 /*
332 * adopt uart open function to open filep (filep is
333 * corresponding to filep of /dev/console)
334 */
335 ret = fops->open(filep);
336 return (ret < 0) ? -EPERM : ret;
337 }
338
UserEndOfRead(CONSOLE_CB * consoleCB,struct file * filep,const struct file_operations_vfs * fops)339 STATIC INLINE VOID UserEndOfRead(CONSOLE_CB *consoleCB, struct file *filep,
340 const struct file_operations_vfs *fops)
341 {
342 CHAR ch;
343 if (consoleCB->consoleTermios.c_lflag & ECHO) {
344 ch = '\r';
345 (VOID)fops->write(filep, &ch, 1);
346 }
347 consoleCB->fifo[consoleCB->fifoIn++] = '\n';
348 consoleCB->fifo[consoleCB->fifoIn] = '\0';
349 consoleCB->currentLen = consoleCB->fifoIn;
350 }
351
352 enum {
353 STAT_NORMAL_KEY,
354 STAT_ESC_KEY,
355 STAT_MULTI_KEY
356 };
357
UserShellCheckUDRL(const CHAR ch,INT32 * lastTokenType)358 STATIC INT32 UserShellCheckUDRL(const CHAR ch, INT32 *lastTokenType)
359 {
360 INT32 ret = LOS_OK;
361 if (ch == 0x1b) { /* 0x1b: ESC */
362 *lastTokenType = STAT_ESC_KEY;
363 return ret;
364 } else if (ch == 0x5b) { /* 0x5b: first Key combination */
365 if (*lastTokenType == STAT_ESC_KEY) {
366 *lastTokenType = STAT_MULTI_KEY;
367 return ret;
368 }
369 } else if (ch == 0x41) { /* up */
370 if (*lastTokenType == STAT_MULTI_KEY) {
371 *lastTokenType = STAT_NORMAL_KEY;
372 return ret;
373 }
374 } else if (ch == 0x42) { /* down */
375 if (*lastTokenType == STAT_MULTI_KEY) {
376 *lastTokenType = STAT_NORMAL_KEY;
377 return ret;
378 }
379 } else if (ch == 0x43) { /* right */
380 if (*lastTokenType == STAT_MULTI_KEY) {
381 *lastTokenType = STAT_NORMAL_KEY;
382 return ret;
383 }
384 } else if (ch == 0x44) { /* left */
385 if (*lastTokenType == STAT_MULTI_KEY) {
386 *lastTokenType = STAT_NORMAL_KEY;
387 return ret;
388 }
389 }
390 return LOS_NOK;
391 }
392
IsNeedContinue(CONSOLE_CB * consoleCB,char ch,INT32 * lastTokenType)393 STATIC INT32 IsNeedContinue(CONSOLE_CB *consoleCB, char ch, INT32 *lastTokenType)
394 {
395 if (((ch == '\b') && (consoleCB->consoleTermios.c_lflag & ECHO) && (ConsoleFifoEmpty(consoleCB))) ||
396 (UserShellCheckUDRL(ch, lastTokenType) == LOS_OK)) { /* parse the up/down/right/left key */
397 return LOS_NOK;
398 }
399
400 return LOS_OK;
401 }
402
EchoToTerminal(CONSOLE_CB * consoleCB,struct file * filep,const struct file_operations_vfs * fops,char ch)403 STATIC VOID EchoToTerminal(CONSOLE_CB *consoleCB, struct file *filep, const struct file_operations_vfs *fops, char ch)
404 {
405 if (consoleCB->consoleTermios.c_lflag & ECHO) {
406 if (ch == '\b') {
407 (VOID)fops->write(filep, "\b \b", 3);
408 } else {
409 (VOID)fops->write(filep, &ch, EACH_CHAR);
410 }
411 }
412 }
413
StoreReadChar(CONSOLE_CB * consoleCB,char ch,INT32 readcount)414 STATIC VOID StoreReadChar(CONSOLE_CB *consoleCB, char ch, INT32 readcount)
415 {
416 /* 3, store read char len need to minus \b */
417 if ((readcount == EACH_CHAR) && (consoleCB->fifoIn <= (CONSOLE_FIFO_SIZE - 3))) {
418 if (ch == '\b') {
419 if (!ConsoleFifoEmpty(consoleCB)) {
420 consoleCB->fifo[--consoleCB->fifoIn] = '\0';
421 }
422 } else {
423 consoleCB->fifo[consoleCB->fifoIn] = (UINT8)ch;
424 consoleCB->fifoIn++;
425 }
426 }
427 }
428
KillPgrp(UINT16 consoleId)429 VOID KillPgrp(UINT16 consoleId)
430 {
431 if ((consoleId > CONSOLE_NUM) || (consoleId <= 0)) {
432 return;
433 }
434
435 CONSOLE_CB *consoleCB = g_console[consoleId - 1];
436 /* the default of consoleCB->pgrpId is -1, may not be set yet, avoid killing all processes */
437 if (consoleCB->pgrpId < 0) {
438 return;
439 }
440 (VOID)OsKillLock(consoleCB->pgrpId, SIGINT);
441 }
442
UserFilepRead(CONSOLE_CB * consoleCB,struct file * filep,const struct file_operations_vfs * fops,CHAR * buffer,size_t bufLen)443 STATIC INT32 UserFilepRead(CONSOLE_CB *consoleCB, struct file *filep, const struct file_operations_vfs *fops,
444 CHAR *buffer, size_t bufLen)
445 {
446 INT32 ret;
447 INT32 needreturn = LOS_NOK;
448 CHAR ch;
449 INT32 lastTokenType = STAT_NORMAL_KEY;
450
451 if (fops->read == NULL) {
452 return -EFAULT;
453 }
454
455 /* Non-ICANON mode */
456 if ((consoleCB->consoleTermios.c_lflag & ICANON) == 0) {
457 ret = fops->read(filep, buffer, bufLen);
458 return (ret < 0) ? -EPERM : ret;
459 }
460 /* ICANON mode: store data to console buffer, read data and stored data into console fifo */
461 if (consoleCB->currentLen == 0) {
462 while (1) {
463 ret = fops->read(filep, &ch, EACH_CHAR);
464 if (ret <= 0) {
465 return ret;
466 }
467
468 if (IsNeedContinue(consoleCB, ch, &lastTokenType))
469 continue;
470
471 switch (ch) {
472 case '\r':
473 ch = '\n';
474 case '\n':
475 EchoToTerminal(consoleCB, filep, fops, ch);
476 UserEndOfRead(consoleCB, filep, fops);
477 ret = ConsoleReadFifo(buffer, consoleCB, bufLen);
478
479 needreturn = LOS_OK;
480 break;
481 case '\b':
482 default:
483 EchoToTerminal(consoleCB, filep, fops, ch);
484 StoreReadChar(consoleCB, ch, ret);
485 break;
486 }
487
488 if (needreturn == LOS_OK)
489 break;
490 }
491 } else {
492 /* if data is already in console fifo, we return them immediately */
493 ret = ConsoleReadFifo(buffer, consoleCB, bufLen);
494 }
495
496 return ret;
497 }
498
FilepRead(struct file * filep,const struct file_operations_vfs * fops,CHAR * buffer,size_t bufLen)499 INT32 FilepRead(struct file *filep, const struct file_operations_vfs *fops, CHAR *buffer, size_t bufLen)
500 {
501 INT32 ret;
502 if (fops->read == NULL) {
503 return -EFAULT;
504 }
505 /*
506 * adopt uart read function to read data from filep
507 * and write data to buffer (filep is
508 * corresponding to filep of /dev/console)
509 */
510 ret = fops->read(filep, buffer, bufLen);
511 return (ret < 0) ? -EPERM : ret;
512 }
513
FilepWrite(struct file * filep,const struct file_operations_vfs * fops,const CHAR * buffer,size_t bufLen)514 INT32 FilepWrite(struct file *filep, const struct file_operations_vfs *fops, const CHAR *buffer, size_t bufLen)
515 {
516 INT32 ret;
517 if (fops->write == NULL) {
518 return -EFAULT;
519 }
520
521 ret = fops->write(filep, buffer, bufLen);
522 return (ret < 0) ? -EPERM : ret;
523 }
524
FilepClose(struct file * filep,const struct file_operations_vfs * fops)525 INT32 FilepClose(struct file *filep, const struct file_operations_vfs *fops)
526 {
527 INT32 ret;
528 if ((fops == NULL) || (fops->close == NULL)) {
529 return -EFAULT;
530 }
531
532 /*
533 * adopt uart close function to open filep (filep is
534 * corresponding to filep of /dev/console)
535 */
536 ret = fops->close(filep);
537 return ret < 0 ? -EPERM : ret;
538 }
539
FilepIoctl(struct file * filep,const struct file_operations_vfs * fops,INT32 cmd,unsigned long arg)540 INT32 FilepIoctl(struct file *filep, const struct file_operations_vfs *fops, INT32 cmd, unsigned long arg)
541 {
542 INT32 ret;
543 if (fops->ioctl == NULL) {
544 return -EFAULT;
545 }
546
547 ret = fops->ioctl(filep, cmd, arg);
548 return (ret < 0) ? -EPERM : ret;
549 }
550
FilepPoll(struct file * filep,const struct file_operations_vfs * fops,poll_table * fds)551 INT32 FilepPoll(struct file *filep, const struct file_operations_vfs *fops, poll_table *fds)
552 {
553 INT32 ret;
554 if (fops->poll == NULL) {
555 return -EFAULT;
556 }
557
558 /*
559 * adopt uart poll function to poll filep (filep is
560 * corresponding to filep of /dev/serial)
561 */
562 ret = fops->poll(filep, fds);
563 return (ret < 0) ? -EPERM : ret;
564 }
565
ConsoleOpen(struct file * filep)566 STATIC INT32 ConsoleOpen(struct file *filep)
567 {
568 INT32 ret;
569 UINT32 consoleID;
570 struct file *privFilep = NULL;
571 const struct file_operations_vfs *fileOps = NULL;
572
573 consoleID = (UINT32)OsConsoleFullpathToID(filep->f_path);
574 if (consoleID == (UINT32)-1) {
575 ret = EPERM;
576 goto ERROUT;
577 }
578 filep->f_priv = g_console[consoleID - 1];
579
580 ret = GetFilepOps(filep, &privFilep, &fileOps);
581 if (ret != ENOERR) {
582 ret = EINVAL;
583 goto ERROUT;
584 }
585 ret = FilepOpen(privFilep, fileOps);
586 if (ret < 0) {
587 ret = EPERM;
588 goto ERROUT;
589 }
590 return ENOERR;
591
592 ERROUT:
593 set_errno(ret);
594 return VFS_ERROR;
595 }
596
ConsoleClose(struct file * filep)597 STATIC INT32 ConsoleClose(struct file *filep)
598 {
599 INT32 ret;
600 struct file *privFilep = NULL;
601 const struct file_operations_vfs *fileOps = NULL;
602
603 ret = GetFilepOps(filep, &privFilep, &fileOps);
604 if (ret != ENOERR) {
605 ret = EINVAL;
606 goto ERROUT;
607 }
608 ret = FilepClose(privFilep, fileOps);
609 if (ret < 0) {
610 ret = EPERM;
611 goto ERROUT;
612 }
613
614 return ENOERR;
615
616 ERROUT:
617 set_errno(ret);
618 return VFS_ERROR;
619 }
620
DoRead(CONSOLE_CB * consoleCB,CHAR * buffer,size_t bufLen,struct file * privFilep,const struct file_operations_vfs * fileOps)621 STATIC ssize_t DoRead(CONSOLE_CB *consoleCB, CHAR *buffer, size_t bufLen,
622 struct file *privFilep,
623 const struct file_operations_vfs *fileOps)
624 {
625 INT32 ret;
626
627 #ifdef LOSCFG_SHELL
628 if (OsCurrTaskGet()->taskID == consoleCB->shellEntryId) {
629 ret = FilepRead(privFilep, fileOps, buffer, bufLen);
630 } else {
631 #endif
632 (VOID)ConsoleCtrlRightsCapture(consoleCB);
633 ret = UserFilepRead(consoleCB, privFilep, fileOps, buffer, bufLen);
634 (VOID)ConsoleCtrlRightsRelease(consoleCB);
635 #ifdef LOSCFG_SHELL
636 }
637 #endif
638
639 return ret;
640 }
641
ConsoleRead(struct file * filep,CHAR * buffer,size_t bufLen)642 STATIC ssize_t ConsoleRead(struct file *filep, CHAR *buffer, size_t bufLen)
643 {
644 INT32 ret;
645 struct file *privFilep = NULL;
646 CONSOLE_CB *consoleCB = NULL;
647 CHAR *sbuffer = NULL;
648 BOOL userBuf = FALSE;
649 const struct file_operations_vfs *fileOps = NULL;
650
651 if ((buffer == NULL) || (bufLen == 0)) {
652 ret = EINVAL;
653 goto ERROUT;
654 }
655
656 if (bufLen > CONSOLE_FIFO_SIZE) {
657 bufLen = CONSOLE_FIFO_SIZE;
658 }
659
660 userBuf = LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buffer, bufLen);
661 ret = GetFilepOps(filep, &privFilep, &fileOps);
662 if (ret != ENOERR) {
663 ret = -EINVAL;
664 goto ERROUT;
665 }
666 consoleCB = (CONSOLE_CB *)filep->f_priv;
667 if (consoleCB == NULL) {
668 consoleCB = OsGetConsoleByTaskID(OsCurrTaskGet()->taskID);
669 if (consoleCB == NULL) {
670 return -EFAULT;
671 }
672 }
673
674 /*
675 * shell task use FilepRead function to get data,
676 * user task use UserFilepRead to get data
677 */
678 sbuffer = userBuf ? LOS_MemAlloc(m_aucSysMem0, bufLen) : buffer;
679 if (sbuffer == NULL) {
680 ret = -ENOMEM;
681 goto ERROUT;
682 }
683
684 ret = DoRead(consoleCB, sbuffer, bufLen, privFilep, fileOps);
685 if (ret < 0) {
686 goto ERROUT;
687 }
688
689 if (userBuf) {
690 if (LOS_ArchCopyToUser(buffer, sbuffer, bufLen) != 0) {
691 ret = -EFAULT;
692 goto ERROUT;
693 }
694
695 LOS_MemFree(m_aucSysMem0, sbuffer);
696 }
697
698 return ret;
699
700 ERROUT:
701 if ((userBuf) && (sbuffer != NULL)) {
702 LOS_MemFree(m_aucSysMem0, sbuffer);
703 }
704 set_errno(-ret);
705 return VFS_ERROR;
706 }
707
DoWrite(CirBufSendCB * cirBufSendCB,CHAR * buffer,size_t bufLen)708 STATIC ssize_t DoWrite(CirBufSendCB *cirBufSendCB, CHAR *buffer, size_t bufLen)
709 {
710 INT32 cnt;
711 size_t written = 0;
712 size_t toWrite = bufLen;
713 UINT32 intSave;
714
715 #ifdef LOSCFG_SHELL_DMESG
716 (VOID)OsLogMemcpyRecord(buffer, bufLen);
717 if (OsCheckConsoleLock()) {
718 return 0;
719 }
720 #endif
721
722 LOS_SpinLockSave(&g_consoleWriteSpinLock, &intSave);
723 while (written < (INT32)bufLen) {
724 /* Transform for CR/LR mode */
725 if ((buffer[written] == '\n') || (buffer[written] == '\r')) {
726 (VOID)LOS_CirBufWrite(&cirBufSendCB->cirBufCB, "\r", 1);
727 }
728
729 cnt = LOS_CirBufWrite(&cirBufSendCB->cirBufCB, &buffer[written], 1);
730 if (cnt <= 0) {
731 break;
732 }
733 toWrite -= cnt;
734 written += cnt;
735 }
736 LOS_SpinUnlockRestore(&g_consoleWriteSpinLock, intSave);
737
738 /* Log is cached but not printed when a system exception occurs */
739 if (OsGetSystemStatus() == OS_SYSTEM_NORMAL) {
740 (VOID)LOS_EventWrite(&cirBufSendCB->sendEvent, CONSOLE_CIRBUF_EVENT);
741 }
742
743 return written;
744 }
745
ConsoleWrite(struct file * filep,const CHAR * buffer,size_t bufLen)746 STATIC ssize_t ConsoleWrite(struct file *filep, const CHAR *buffer, size_t bufLen)
747 {
748 INT32 ret;
749 CHAR *sbuffer = NULL;
750 BOOL userBuf = FALSE;
751 CirBufSendCB *cirBufSendCB = NULL;
752 struct file *privFilep = NULL;
753 const struct file_operations_vfs *fileOps = NULL;
754
755 if ((buffer == NULL) || (bufLen == 0)) {
756 ret = EINVAL;
757 goto ERROUT;
758 }
759
760 if (bufLen > CONSOLE_FIFO_SIZE) {
761 bufLen = CONSOLE_FIFO_SIZE;
762 }
763
764 userBuf = LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buffer, bufLen);
765
766 ret = GetFilepOps(filep, &privFilep, &fileOps);
767 if ((ret != ENOERR) || (fileOps->write == NULL) || (filep->f_priv == NULL)) {
768 ret = EINVAL;
769 goto ERROUT;
770 }
771 cirBufSendCB = ((CONSOLE_CB *)filep->f_priv)->cirBufSendCB;
772
773 /*
774 * adopt uart open function to read data from buffer
775 * and write data to filep (filep is
776 * corresponding to filep of /dev/console)
777 */
778 sbuffer = userBuf ? LOS_MemAlloc(m_aucSysMem0, bufLen) : (CHAR *)buffer;
779 if (sbuffer == NULL) {
780 ret = ENOMEM;
781 goto ERROUT;
782 }
783
784 if (userBuf && (LOS_ArchCopyFromUser(sbuffer, buffer, bufLen) != 0)) {
785 ret = EFAULT;
786 goto ERROUT;
787 }
788 ret = DoWrite(cirBufSendCB, sbuffer, bufLen);
789
790 if (userBuf) {
791 LOS_MemFree(m_aucSysMem0, sbuffer);
792 }
793
794 return ret;
795
796 ERROUT:
797 if (userBuf && sbuffer != NULL) {
798 LOS_MemFree(m_aucSysMem0, sbuffer);
799 }
800
801 set_errno(ret);
802 return VFS_ERROR;
803 }
804
ConsoleSetSW(CONSOLE_CB * consoleCB,unsigned long arg)805 STATIC INT32 ConsoleSetSW(CONSOLE_CB *consoleCB, unsigned long arg)
806 {
807 struct termios kerTermios;
808 UINT32 intSave;
809
810 if (LOS_ArchCopyFromUser(&kerTermios, (struct termios *)arg, sizeof(struct termios)) != 0) {
811 return -EFAULT;
812 }
813
814 LOS_SpinLockSave(&g_consoleSpin, &intSave);
815 (VOID)ConsoleTcSetAttr(consoleCB->fd, 0, &kerTermios);
816 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
817 return LOS_OK;
818 }
819
820 #define DEFAULT_WINDOW_SIZE_COL 80
821 #define DEFAULT_WINDOW_SIZE_ROW 24
ConsoleGetWinSize(unsigned long arg)822 STATIC INT32 ConsoleGetWinSize(unsigned long arg)
823 {
824 struct winsize kws = {
825 .ws_col = DEFAULT_WINDOW_SIZE_COL,
826 .ws_row = DEFAULT_WINDOW_SIZE_ROW
827 };
828
829 return (LOS_CopyFromKernel((VOID *)arg, sizeof(struct winsize), &kws, sizeof(struct winsize)) != 0) ?
830 -EFAULT : LOS_OK;
831 }
832
ConsoleGetTermios(unsigned long arg)833 STATIC INT32 ConsoleGetTermios(unsigned long arg)
834 {
835 struct file *filep = NULL;
836 CONSOLE_CB *consoleCB = NULL;
837
838 INT32 ret = fs_getfilep(0, &filep);
839 if (ret < 0) {
840 return -EPERM;
841 }
842
843 consoleCB = (CONSOLE_CB *)filep->f_priv;
844 if (consoleCB == NULL) {
845 return -EFAULT;
846 }
847
848 return (LOS_ArchCopyToUser((VOID *)arg, &consoleCB->consoleTermios, sizeof(struct termios)) != 0) ?
849 -EFAULT : LOS_OK;
850 }
851
ConsoleSetPgrp(CONSOLE_CB * consoleCB,unsigned long arg)852 INT32 ConsoleSetPgrp(CONSOLE_CB *consoleCB, unsigned long arg)
853 {
854 if (LOS_ArchCopyFromUser(&consoleCB->pgrpId, (INT32 *)(UINTPTR)arg, sizeof(INT32)) != 0) {
855 return -EFAULT;
856 }
857 return LOS_OK;
858 }
859
ConsoleGetPgrp(CONSOLE_CB * consoleCB,unsigned long arg)860 INT32 ConsoleGetPgrp(CONSOLE_CB *consoleCB, unsigned long arg)
861 {
862 return (LOS_ArchCopyToUser((VOID *)arg, &consoleCB->pgrpId, sizeof(INT32)) != 0) ? -EFAULT : LOS_OK;
863 }
864
ConsoleIoctl(struct file * filep,INT32 cmd,unsigned long arg)865 STATIC INT32 ConsoleIoctl(struct file *filep, INT32 cmd, unsigned long arg)
866 {
867 INT32 ret;
868 struct file *privFilep = NULL;
869 CONSOLE_CB *consoleCB = NULL;
870 const struct file_operations_vfs *fileOps = NULL;
871
872 ret = GetFilepOps(filep, &privFilep, &fileOps);
873 if (ret != ENOERR) {
874 ret = EINVAL;
875 goto ERROUT;
876 }
877
878 if (fileOps->ioctl == NULL) {
879 ret = EFAULT;
880 goto ERROUT;
881 }
882
883 consoleCB = (CONSOLE_CB *)filep->f_priv;
884 if (consoleCB == NULL) {
885 ret = EINVAL;
886 goto ERROUT;
887 }
888
889 switch (cmd) {
890 case CONSOLE_CONTROL_RIGHTS_CAPTURE:
891 ret = ConsoleCtrlRightsCapture(consoleCB);
892 break;
893 case CONSOLE_CONTROL_RIGHTS_RELEASE:
894 ret = ConsoleCtrlRightsRelease(consoleCB);
895 break;
896 case CONSOLE_CONTROL_CAPTURE_LINE:
897 ret = ConsoleCtrlCaptureLine(consoleCB);
898 break;
899 case CONSOLE_CONTROL_CAPTURE_CHAR:
900 ret = ConsoleCtrlCaptureChar(consoleCB);
901 break;
902 case CONSOLE_CONTROL_REG_USERTASK:
903 ret = ConsoleTaskReg(consoleCB->consoleID, arg);
904 break;
905 case TIOCGWINSZ:
906 ret = ConsoleGetWinSize(arg);
907 break;
908 case TCSETSW:
909 ret = ConsoleSetSW(consoleCB, arg);
910 break;
911 case TCGETS:
912 ret = ConsoleGetTermios(arg);
913 break;
914 case TIOCGPGRP:
915 ret = ConsoleGetPgrp(consoleCB, arg);
916 break;
917 case TIOCSPGRP:
918 ret = ConsoleSetPgrp(consoleCB, arg);
919 break;
920 default:
921 if ((cmd == UART_CFG_ATTR || cmd == UART_CFG_PRIVATE)
922 && !LOS_IsUserAddress(arg)) {
923 ret = EINVAL;
924 goto ERROUT;
925 }
926 ret = fileOps->ioctl(privFilep, cmd, arg);
927 break;
928 }
929
930 if (ret < 0) {
931 ret = EPERM;
932 goto ERROUT;
933 }
934
935 return ret;
936 ERROUT:
937 set_errno(ret);
938 return VFS_ERROR;
939 }
940
ConsolePoll(struct file * filep,poll_table * fds)941 STATIC INT32 ConsolePoll(struct file *filep, poll_table *fds)
942 {
943 INT32 ret;
944 struct file *privFilep = NULL;
945 const struct file_operations_vfs *fileOps = NULL;
946
947 ret = GetFilepOps(filep, &privFilep, &fileOps);
948 if (ret != ENOERR) {
949 ret = EINVAL;
950 goto ERROUT;
951 }
952
953 ret = FilepPoll(privFilep, fileOps, fds);
954 if (ret < 0) {
955 ret = EPERM;
956 goto ERROUT;
957 }
958 return ret;
959
960 ERROUT:
961 set_errno(ret);
962 return VFS_ERROR;
963 }
964
965 /* console device driver function structure */
966 STATIC const struct file_operations_vfs g_consoleDevOps = {
967 .open = ConsoleOpen, /* open */
968 .close = ConsoleClose, /* close */
969 .read = ConsoleRead, /* read */
970 .write = ConsoleWrite, /* write */
971 .seek = NULL,
972 .ioctl = ConsoleIoctl,
973 .mmap = NULL,
974 #ifndef CONFIG_DISABLE_POLL
975 .poll = ConsolePoll,
976 #endif
977 };
978
OsConsoleTermiosInit(CONSOLE_CB * consoleCB,const CHAR * deviceName)979 STATIC VOID OsConsoleTermiosInit(CONSOLE_CB *consoleCB, const CHAR *deviceName)
980 {
981 struct termios consoleTermios = {0};
982
983 if ((deviceName != NULL) &&
984 (strlen(deviceName) == strlen(SERIAL)) &&
985 (!strncmp(deviceName, SERIAL, strlen(SERIAL)))) {
986 consoleCB->isNonBlock = SetSerialBlock(consoleCB);
987
988 /* set console to have a buffer for user */
989 (VOID)ConsoleTcGetAttr(consoleCB->fd, &consoleTermios);
990 consoleTermios.c_lflag |= ICANON | ECHO;
991 consoleTermios.c_cc[VINTR] = 3; /* /003 for ^C */
992 (VOID)ConsoleTcSetAttr(consoleCB->fd, 0, &consoleTermios);
993 }
994 #ifdef LOSCFG_NET_TELNET
995 else if ((deviceName != NULL) &&
996 (strlen(deviceName) == strlen(TELNET)) &&
997 (!strncmp(deviceName, TELNET, strlen(TELNET)))) {
998 consoleCB->isNonBlock = SetTelnetBlock(consoleCB);
999 /* set console to have a buffer for user */
1000 (VOID)ConsoleTcGetAttr(consoleCB->fd, &consoleTermios);
1001 consoleTermios.c_lflag |= ICANON | ECHO;
1002 consoleTermios.c_cc[VINTR] = 3; /* /003 for ^C */
1003 (VOID)ConsoleTcSetAttr(consoleCB->fd, 0, &consoleTermios);
1004 }
1005 #endif
1006 }
1007
OsConsoleFileInit(CONSOLE_CB * consoleCB)1008 STATIC INT32 OsConsoleFileInit(CONSOLE_CB *consoleCB)
1009 {
1010 INT32 ret;
1011 struct Vnode *vnode = NULL;
1012 struct file *filep = NULL;
1013
1014 VnodeHold();
1015 ret = VnodeLookup(consoleCB->name, &vnode, 0);
1016 if (ret != LOS_OK) {
1017 ret = EACCES;
1018 goto ERROUT;
1019 }
1020
1021 filep = files_allocate(vnode, O_RDWR, 0, consoleCB, FILE_START_FD);
1022 if (filep == NULL) {
1023 ret = EMFILE;
1024 goto ERROUT;
1025 }
1026 filep->ops = (struct file_operations_vfs *)((struct drv_data *)vnode->data)->ops;
1027 consoleCB->fd = filep->fd;
1028
1029 ERROUT:
1030 VnodeDrop();
1031 return ret;
1032 }
1033
1034 /*
1035 * Initialized console control platform so that when we operate /dev/console
1036 * as if we are operating /dev/ttyS0 (uart0).
1037 */
OsConsoleDevInit(CONSOLE_CB * consoleCB,const CHAR * deviceName)1038 STATIC INT32 OsConsoleDevInit(CONSOLE_CB *consoleCB, const CHAR *deviceName)
1039 {
1040 INT32 ret;
1041 struct file *filep = NULL;
1042 struct Vnode *vnode = NULL;
1043 struct file_operations_vfs *devOps = NULL;
1044
1045 /* allocate memory for filep,in order to unchange the value of filep */
1046 filep = (struct file *)LOS_MemAlloc(m_aucSysMem0, sizeof(struct file));
1047 if (filep == NULL) {
1048 ret = ENOMEM;
1049 goto ERROUT;
1050 }
1051
1052 VnodeHold();
1053 ret = VnodeLookup(deviceName, &vnode, V_DUMMY);
1054 VnodeDrop(); // not correct, but can't fix perfectly here
1055 if (ret != LOS_OK) {
1056 ret = EACCES;
1057 PRINTK("!! can not find %s\n", consoleCB->name);
1058 goto ERROUT;
1059 }
1060
1061 consoleCB->devVnode = vnode;
1062
1063 /*
1064 * initialize the console filep which is associated with /dev/console,
1065 * assign the uart0 vnode of /dev/ttyS0 to console inod of /dev/console,
1066 * then we can operate console's filep as if we operate uart0 filep of
1067 * /dev/ttyS0.
1068 */
1069 (VOID)memset_s(filep, sizeof(struct file), 0, sizeof(struct file));
1070 filep->f_oflags = O_RDWR;
1071 filep->f_pos = 0;
1072 filep->f_vnode = vnode;
1073 filep->f_path = NULL;
1074 filep->f_priv = NULL;
1075 /*
1076 * Use filep to connect console and uart, we can find uart driver function through filep.
1077 * now we can operate /dev/console to operate /dev/ttyS0 through filep.
1078 */
1079 devOps = (struct file_operations_vfs *)((struct drv_data*)vnode->data)->ops;
1080 if (devOps != NULL && devOps->open != NULL) {
1081 (VOID)devOps->open(filep);
1082 } else {
1083 ret = ENOSYS;
1084 goto ERROUT;
1085 }
1086
1087 ret = register_driver(consoleCB->name, &g_consoleDevOps, DEFFILEMODE, filep);
1088 if (ret != LOS_OK) {
1089 goto ERROUT;
1090 }
1091
1092 return LOS_OK;
1093
1094 ERROUT:
1095 if (filep) {
1096 (VOID)LOS_MemFree(m_aucSysMem0, filep);
1097 }
1098
1099 set_errno(ret);
1100 return LOS_NOK;
1101 }
1102
OsConsoleDevDeinit(const CONSOLE_CB * consoleCB)1103 STATIC UINT32 OsConsoleDevDeinit(const CONSOLE_CB *consoleCB)
1104 {
1105 return unregister_driver(consoleCB->name);
1106 }
1107
ConsoleCirBufCreate(VOID)1108 STATIC CirBufSendCB *ConsoleCirBufCreate(VOID)
1109 {
1110 UINT32 ret;
1111 CHAR *fifo = NULL;
1112 CirBufSendCB *cirBufSendCB = NULL;
1113 CirBuf *cirBufCB = NULL;
1114
1115 cirBufSendCB = (CirBufSendCB *)LOS_MemAlloc(m_aucSysMem0, sizeof(CirBufSendCB));
1116 if (cirBufSendCB == NULL) {
1117 return NULL;
1118 }
1119 (VOID)memset_s(cirBufSendCB, sizeof(CirBufSendCB), 0, sizeof(CirBufSendCB));
1120
1121 fifo = (CHAR *)LOS_MemAlloc(m_aucSysMem0, CONSOLE_CIRCBUF_SIZE);
1122 if (fifo == NULL) {
1123 goto ERROR_WITH_SENDCB;
1124 }
1125 (VOID)memset_s(fifo, CONSOLE_CIRCBUF_SIZE, 0, CONSOLE_CIRCBUF_SIZE);
1126
1127 cirBufCB = &cirBufSendCB->cirBufCB;
1128 ret = LOS_CirBufInit(cirBufCB, fifo, CONSOLE_CIRCBUF_SIZE);
1129 if (ret != LOS_OK) {
1130 goto ERROR_WITH_FIFO;
1131 }
1132
1133 (VOID)LOS_EventInit(&cirBufSendCB->sendEvent);
1134 return cirBufSendCB;
1135
1136 ERROR_WITH_FIFO:
1137 (VOID)LOS_MemFree(m_aucSysMem0, cirBufCB->fifo);
1138 ERROR_WITH_SENDCB:
1139 (VOID)LOS_MemFree(m_aucSysMem0, cirBufSendCB);
1140 return NULL;
1141 }
1142
ConsoleCirBufDelete(CirBufSendCB * cirBufSendCB)1143 STATIC VOID ConsoleCirBufDelete(CirBufSendCB *cirBufSendCB)
1144 {
1145 CirBuf *cirBufCB = &cirBufSendCB->cirBufCB;
1146
1147 (VOID)LOS_MemFree(m_aucSysMem0, cirBufCB->fifo);
1148 LOS_CirBufDeinit(cirBufCB);
1149 (VOID)LOS_EventDestroy(&cirBufSendCB->sendEvent);
1150 (VOID)LOS_MemFree(m_aucSysMem0, cirBufSendCB);
1151 }
1152
OsConsoleBufInit(CONSOLE_CB * consoleCB)1153 STATIC UINT32 OsConsoleBufInit(CONSOLE_CB *consoleCB)
1154 {
1155 UINT32 ret;
1156 TSK_INIT_PARAM_S initParam = {0};
1157
1158 consoleCB->cirBufSendCB = ConsoleCirBufCreate();
1159 if (consoleCB->cirBufSendCB == NULL) {
1160 return LOS_NOK;
1161 }
1162
1163 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)ConsoleSendTask;
1164 initParam.usTaskPrio = SHELL_TASK_PRIORITY;
1165 initParam.auwArgs[0] = (UINTPTR)consoleCB;
1166 initParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
1167 initParam.pcName = (consoleCB->consoleID == CONSOLE_SERIAL) ? "SendToSer" : "SendToTelnet";
1168 initParam.uwResved = LOS_TASK_STATUS_DETACHED;
1169
1170 ret = LOS_TaskCreate(&consoleCB->sendTaskID, &initParam);
1171 if (ret != LOS_OK) {
1172 ConsoleCirBufDelete(consoleCB->cirBufSendCB);
1173 consoleCB->cirBufSendCB = NULL;
1174 return LOS_NOK;
1175 }
1176 (VOID)LOS_EventRead(&consoleCB->cirBufSendCB->sendEvent, CONSOLE_SEND_TASK_RUNNING,
1177 LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
1178
1179 return LOS_OK;
1180 }
1181
OsConsoleBufDeinit(CONSOLE_CB * consoleCB)1182 STATIC VOID OsConsoleBufDeinit(CONSOLE_CB *consoleCB)
1183 {
1184 CirBufSendCB *cirBufSendCB = consoleCB->cirBufSendCB;
1185
1186 consoleCB->cirBufSendCB = NULL;
1187 (VOID)LOS_EventWrite(&cirBufSendCB->sendEvent, CONSOLE_SEND_TASK_EXIT);
1188 }
1189
OsConsoleCBInit(UINT32 consoleID)1190 STATIC CONSOLE_CB *OsConsoleCBInit(UINT32 consoleID)
1191 {
1192 CONSOLE_CB *consoleCB = (CONSOLE_CB *)LOS_MemAlloc((VOID *)m_aucSysMem0, sizeof(CONSOLE_CB));
1193 if (consoleCB == NULL) {
1194 return NULL;
1195 }
1196 (VOID)memset_s(consoleCB, sizeof(CONSOLE_CB), 0, sizeof(CONSOLE_CB));
1197
1198 consoleCB->consoleID = consoleID;
1199 consoleCB->pgrpId = -1;
1200 consoleCB->shellEntryId = SHELL_ENTRYID_INVALID; /* initialize shellEntryId to an invalid value */
1201 consoleCB->name = LOS_MemAlloc((VOID *)m_aucSysMem0, CONSOLE_NAMELEN);
1202 if (consoleCB->name == NULL) {
1203 PRINT_ERR("consoleCB->name malloc failed\n");
1204 (VOID)LOS_MemFree((VOID *)m_aucSysMem0, consoleCB);
1205 return NULL;
1206 }
1207 return consoleCB;
1208 }
1209
OsConsoleCBDeinit(CONSOLE_CB * consoleCB)1210 STATIC VOID OsConsoleCBDeinit(CONSOLE_CB *consoleCB)
1211 {
1212 (VOID)LOS_MemFree((VOID *)m_aucSysMem0, consoleCB->name);
1213 consoleCB->name = NULL;
1214 (VOID)LOS_MemFree((VOID *)m_aucSysMem0, consoleCB);
1215 }
1216
OsConsoleCreate(UINT32 consoleID,const CHAR * deviceName)1217 STATIC CONSOLE_CB *OsConsoleCreate(UINT32 consoleID, const CHAR *deviceName)
1218 {
1219 INT32 ret;
1220 CONSOLE_CB *consoleCB = OsConsoleCBInit(consoleID);
1221 if (consoleCB == NULL) {
1222 PRINT_ERR("console malloc error.\n");
1223 return NULL;
1224 }
1225
1226 ret = snprintf_s(consoleCB->name, CONSOLE_NAMELEN, CONSOLE_NAMELEN - 1,
1227 "%s%u", CONSOLE, consoleCB->consoleID);
1228 if (ret == -1) {
1229 PRINT_ERR("consoleCB->name snprintf_s failed\n");
1230 goto ERR_WITH_NAME;
1231 }
1232
1233 ret = (INT32)OsConsoleBufInit(consoleCB);
1234 if (ret != LOS_OK) {
1235 PRINT_ERR("console OsConsoleBufInit error. %d\n", ret);
1236 goto ERR_WITH_NAME;
1237 }
1238
1239 ret = (INT32)LOS_SemCreate(1, &consoleCB->consoleSem);
1240 if (ret != LOS_OK) {
1241 PRINT_ERR("creat sem for uart failed\n");
1242 goto ERR_WITH_BUF;
1243 }
1244
1245 ret = OsConsoleDevInit(consoleCB, deviceName);
1246 if (ret != LOS_OK) {
1247 PRINT_ERR("console OsConsoleDevInit error. %d\n", ret);
1248 goto ERR_WITH_SEM;
1249 }
1250
1251 ret = OsConsoleFileInit(consoleCB);
1252 if (ret != LOS_OK) {
1253 PRINT_ERR("console OsConsoleFileInit error. %d\n", ret);
1254 goto ERR_WITH_DEV;
1255 }
1256
1257 OsConsoleTermiosInit(consoleCB, deviceName);
1258 return consoleCB;
1259
1260 ERR_WITH_DEV:
1261 ret = (INT32)OsConsoleDevDeinit(consoleCB);
1262 if (ret != LOS_OK) {
1263 PRINT_ERR("OsConsoleDevDeinit failed!\n");
1264 }
1265 ERR_WITH_SEM:
1266 (VOID)LOS_SemDelete(consoleCB->consoleSem);
1267 ERR_WITH_BUF:
1268 OsConsoleBufDeinit(consoleCB);
1269 ERR_WITH_NAME:
1270 OsConsoleCBDeinit(consoleCB);
1271 return NULL;
1272 }
1273
OsConsoleDelete(CONSOLE_CB * consoleCB)1274 STATIC UINT32 OsConsoleDelete(CONSOLE_CB *consoleCB)
1275 {
1276 UINT32 ret;
1277
1278 (VOID)files_close(consoleCB->fd);
1279 ret = OsConsoleDevDeinit(consoleCB);
1280 if (ret != LOS_OK) {
1281 PRINT_ERR("OsConsoleDevDeinit failed!\n");
1282 }
1283 OsConsoleBufDeinit((CONSOLE_CB *)consoleCB);
1284 (VOID)LOS_SemDelete(consoleCB->consoleSem);
1285 (VOID)LOS_MemFree(m_aucSysMem0, consoleCB->name);
1286 consoleCB->name = NULL;
1287 (VOID)LOS_MemFree(m_aucSysMem0, consoleCB);
1288
1289 return ret;
1290 }
1291
1292 /* Initialized system console and return stdinfd stdoutfd stderrfd */
system_console_init(const CHAR * deviceName)1293 INT32 system_console_init(const CHAR *deviceName)
1294 {
1295 #ifdef LOSCFG_SHELL
1296 UINT32 ret;
1297 #endif
1298 INT32 consoleID;
1299 UINT32 intSave;
1300 CONSOLE_CB *consoleCB = NULL;
1301
1302 consoleID = OsGetConsoleID(deviceName);
1303 if (consoleID == -1) {
1304 PRINT_ERR("device is full.\n");
1305 return VFS_ERROR;
1306 }
1307
1308 consoleCB = OsConsoleCreate((UINT32)consoleID, deviceName);
1309 if (consoleCB == NULL) {
1310 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1311 return VFS_ERROR;
1312 }
1313
1314 LOS_SpinLockSave(&g_consoleSpin, &intSave);
1315 g_console[consoleID - 1] = consoleCB;
1316 if (OsCurrTaskGet() != NULL) {
1317 g_taskConsoleIDArray[OsCurrTaskGet()->taskID] = (UINT8)consoleID;
1318 }
1319 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
1320
1321 #ifdef LOSCFG_SHELL
1322 ret = OsShellInit(consoleID);
1323 if (ret != LOS_OK) {
1324 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1325 LOS_SpinLockSave(&g_consoleSpin, &intSave);
1326 (VOID)OsConsoleDelete(consoleCB);
1327 g_console[consoleID - 1] = NULL;
1328 if (OsCurrTaskGet() != NULL) {
1329 g_taskConsoleIDArray[OsCurrTaskGet()->taskID] = 0;
1330 }
1331 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
1332 return VFS_ERROR;
1333 }
1334 #endif
1335
1336 return ENOERR;
1337 }
1338
system_console_deinit(const CHAR * deviceName)1339 INT32 system_console_deinit(const CHAR *deviceName)
1340 {
1341 UINT32 ret;
1342 CONSOLE_CB *consoleCB = NULL;
1343 UINT32 taskIdx;
1344 LosTaskCB *taskCB = NULL;
1345 UINT32 intSave;
1346
1347 consoleCB = OsGetConsoleByDevice(deviceName);
1348 if (consoleCB == NULL) {
1349 return VFS_ERROR;
1350 }
1351
1352 #ifdef LOSCFG_SHELL
1353 (VOID)OsShellDeinit((INT32)consoleCB->consoleID);
1354 #endif
1355
1356 LOS_SpinLockSave(&g_consoleSpin, &intSave);
1357 /* Redirect all tasks to serial as telnet was unavailable after deinitializing */
1358 for (taskIdx = 0; taskIdx < g_taskMaxNum; taskIdx++) {
1359 taskCB = ((LosTaskCB *)g_taskCBArray) + taskIdx;
1360 if (OsTaskIsUnused(taskCB)) {
1361 continue;
1362 } else {
1363 g_taskConsoleIDArray[taskCB->taskID] = CONSOLE_SERIAL;
1364 }
1365 }
1366 g_console[consoleCB->consoleID - 1] = NULL;
1367 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
1368
1369 ret = OsConsoleDelete(consoleCB);
1370 if (ret != LOS_OK) {
1371 PRINT_ERR("%s, Failed to system_console_deinit\n", __FUNCTION__);
1372 return VFS_ERROR;
1373 }
1374
1375 return ENOERR;
1376 }
1377
ConsoleEnable(VOID)1378 BOOL ConsoleEnable(VOID)
1379 {
1380 INT32 consoleID;
1381
1382 if (OsCurrTaskGet() != NULL) {
1383 consoleID = g_taskConsoleIDArray[OsCurrTaskGet()->taskID];
1384 if (g_uart_fputc_en == 0) {
1385 if ((g_console[CONSOLE_TELNET - 1] != NULL) && OsPreemptable()) {
1386 return TRUE;
1387 }
1388 }
1389
1390 if (consoleID == 0) {
1391 return FALSE;
1392 } else if ((consoleID == CONSOLE_TELNET) || (consoleID == CONSOLE_SERIAL)) {
1393 return ((OsGetSystemStatus() == OS_SYSTEM_NORMAL) && !OsPreemptable()) ? FALSE : TRUE;
1394 }
1395 #if defined (LOSCFG_DRIVERS_USB_SERIAL_GADGET) || defined (LOSCFG_DRIVERS_USB_ETH_SER_GADGET)
1396 else if ((SerialTypeGet() == SERIAL_TYPE_USBTTY_DEV) && (userial_mask_get() == 1)) {
1397 return TRUE;
1398 }
1399 #endif
1400 }
1401
1402 return FALSE;
1403 }
1404
IsShellEntryRunning(UINT32 shellEntryId)1405 BOOL IsShellEntryRunning(UINT32 shellEntryId)
1406 {
1407 LosTaskCB *taskCB = NULL;
1408 if (shellEntryId == SHELL_ENTRYID_INVALID) {
1409 return FALSE;
1410 }
1411 taskCB = OsGetTaskCB(shellEntryId);
1412 return !OsTaskIsUnused(taskCB) &&
1413 (strlen(taskCB->taskName) == SHELL_ENTRY_NAME_LEN &&
1414 strncmp(taskCB->taskName, SHELL_ENTRY_NAME, SHELL_ENTRY_NAME_LEN) == 0);
1415 }
1416
ConsoleTaskReg(INT32 consoleID,UINT32 taskID)1417 INT32 ConsoleTaskReg(INT32 consoleID, UINT32 taskID)
1418 {
1419 UINT32 intSave;
1420
1421 LOS_SpinLockSave(&g_consoleSpin, &intSave);
1422 if (!IsShellEntryRunning(g_console[consoleID - 1]->shellEntryId)) {
1423 g_console[consoleID - 1]->shellEntryId = taskID;
1424 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
1425 (VOID)OsSetCurrProcessGroupID(OsGetUserInitProcessID());
1426 return LOS_OK;
1427 }
1428 LOS_SpinUnlockRestore(&g_consoleSpin, intSave);
1429 return (g_console[consoleID - 1]->shellEntryId == taskID) ? LOS_OK : LOS_NOK;
1430 }
1431
SetSerialNonBlock(const CONSOLE_CB * consoleCB)1432 BOOL SetSerialNonBlock(const CONSOLE_CB *consoleCB)
1433 {
1434 if (consoleCB == NULL) {
1435 PRINT_ERR("%s: Input parameter is illegal\n", __FUNCTION__);
1436 return FALSE;
1437 }
1438 return ioctl(consoleCB->fd, CONSOLE_CMD_RD_BLOCK_SERIAL, CONSOLE_RD_NONBLOCK) == 0;
1439 }
1440
SetSerialBlock(const CONSOLE_CB * consoleCB)1441 BOOL SetSerialBlock(const CONSOLE_CB *consoleCB)
1442 {
1443 if (consoleCB == NULL) {
1444 PRINT_ERR("%s: Input parameter is illegal\n", __FUNCTION__);
1445 return TRUE;
1446 }
1447 return ioctl(consoleCB->fd, CONSOLE_CMD_RD_BLOCK_SERIAL, CONSOLE_RD_BLOCK) != 0;
1448 }
1449
SetTelnetNonBlock(const CONSOLE_CB * consoleCB)1450 BOOL SetTelnetNonBlock(const CONSOLE_CB *consoleCB)
1451 {
1452 if (consoleCB == NULL) {
1453 PRINT_ERR("%s: Input parameter is illegal\n", __FUNCTION__);
1454 return FALSE;
1455 }
1456 return ioctl(consoleCB->fd, CONSOLE_CMD_RD_BLOCK_TELNET, CONSOLE_RD_NONBLOCK) == 0;
1457 }
1458
SetTelnetBlock(const CONSOLE_CB * consoleCB)1459 BOOL SetTelnetBlock(const CONSOLE_CB *consoleCB)
1460 {
1461 if (consoleCB == NULL) {
1462 PRINT_ERR("%s: Input parameter is illegal\n", __FUNCTION__);
1463 return TRUE;
1464 }
1465 return ioctl(consoleCB->fd, CONSOLE_CMD_RD_BLOCK_TELNET, CONSOLE_RD_BLOCK) != 0;
1466 }
1467
is_nonblock(const CONSOLE_CB * consoleCB)1468 BOOL is_nonblock(const CONSOLE_CB *consoleCB)
1469 {
1470 if (consoleCB == NULL) {
1471 PRINT_ERR("%s: Input parameter is illegal\n", __FUNCTION__);
1472 return FALSE;
1473 }
1474 return consoleCB->isNonBlock;
1475 }
1476
ConsoleUpdateFd(VOID)1477 INT32 ConsoleUpdateFd(VOID)
1478 {
1479 INT32 consoleID;
1480
1481 if (OsCurrTaskGet() != NULL) {
1482 consoleID = g_taskConsoleIDArray[(OsCurrTaskGet())->taskID];
1483 } else {
1484 return -1;
1485 }
1486
1487 if (g_uart_fputc_en == 0) {
1488 if (g_console[CONSOLE_TELNET - 1] != NULL) {
1489 consoleID = CONSOLE_TELNET;
1490 }
1491 } else if (consoleID == 0) {
1492 if (g_console[CONSOLE_SERIAL - 1] != NULL) {
1493 consoleID = CONSOLE_SERIAL;
1494 } else if (g_console[CONSOLE_TELNET - 1] != NULL) {
1495 consoleID = CONSOLE_TELNET;
1496 } else {
1497 PRINTK("No console dev used.\n");
1498 return -1;
1499 }
1500 }
1501
1502 return (g_console[consoleID - 1] != NULL) ? g_console[consoleID - 1]->fd : -1;
1503 }
1504
OsGetConsoleByID(INT32 consoleID)1505 CONSOLE_CB *OsGetConsoleByID(INT32 consoleID)
1506 {
1507 if (consoleID != CONSOLE_TELNET) {
1508 consoleID = CONSOLE_SERIAL;
1509 }
1510 return g_console[consoleID - 1];
1511 }
1512
OsGetConsoleByTaskID(UINT32 taskID)1513 CONSOLE_CB *OsGetConsoleByTaskID(UINT32 taskID)
1514 {
1515 INT32 consoleID = g_taskConsoleIDArray[taskID];
1516
1517 return OsGetConsoleByID(consoleID);
1518 }
1519
OsSetConsoleID(UINT32 newTaskID,UINT32 curTaskID)1520 VOID OsSetConsoleID(UINT32 newTaskID, UINT32 curTaskID)
1521 {
1522 if ((newTaskID >= LOSCFG_BASE_CORE_TSK_LIMIT) || (curTaskID >= LOSCFG_BASE_CORE_TSK_LIMIT)) {
1523 return;
1524 }
1525
1526 g_taskConsoleIDArray[newTaskID] = g_taskConsoleIDArray[curTaskID];
1527 }
1528
WriteToTerminal(const CONSOLE_CB * consoleCB,const CHAR * buffer,size_t bufLen)1529 STATIC ssize_t WriteToTerminal(const CONSOLE_CB *consoleCB, const CHAR *buffer, size_t bufLen)
1530 {
1531 INT32 ret, fd;
1532 INT32 cnt = 0;
1533 struct file *privFilep = NULL;
1534 struct file *filep = NULL;
1535 const struct file_operations_vfs *fileOps = NULL;
1536
1537 fd = consoleCB->fd;
1538 ret = fs_getfilep(fd, &filep);
1539 ret = GetFilepOps(filep, &privFilep, &fileOps);
1540
1541 if ((fileOps == NULL) || (fileOps->write == NULL)) {
1542 ret = EFAULT;
1543 goto ERROUT;
1544 }
1545 (VOID)fileOps->write(privFilep, buffer, bufLen);
1546
1547 return cnt;
1548
1549 ERROUT:
1550 set_errno(ret);
1551 return VFS_ERROR;
1552 }
1553
ConsoleSendTask(UINTPTR param)1554 STATIC UINT32 ConsoleSendTask(UINTPTR param)
1555 {
1556 CONSOLE_CB *consoleCB = (CONSOLE_CB *)param;
1557 CirBufSendCB *cirBufSendCB = consoleCB->cirBufSendCB;
1558 CirBuf *cirBufCB = &cirBufSendCB->cirBufCB;
1559 UINT32 ret, size;
1560 CHAR *buf = NULL;
1561
1562 (VOID)LOS_EventWrite(&cirBufSendCB->sendEvent, CONSOLE_SEND_TASK_RUNNING);
1563
1564 while (1) {
1565 ret = LOS_EventRead(&cirBufSendCB->sendEvent, CONSOLE_CIRBUF_EVENT | CONSOLE_SEND_TASK_EXIT,
1566 LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
1567 if (ret == CONSOLE_CIRBUF_EVENT) {
1568 size = LOS_CirBufUsedSize(cirBufCB);
1569 if (size == 0) {
1570 continue;
1571 }
1572 buf = (CHAR *)LOS_MemAlloc(m_aucSysMem1, size + 1);
1573 if (buf == NULL) {
1574 continue;
1575 }
1576 (VOID)memset_s(buf, size + 1, 0, size + 1);
1577
1578 (VOID)LOS_CirBufRead(cirBufCB, buf, size);
1579
1580 (VOID)WriteToTerminal(consoleCB, buf, size);
1581 (VOID)LOS_MemFree(m_aucSysMem1, buf);
1582 } else if (ret == CONSOLE_SEND_TASK_EXIT) {
1583 break;
1584 }
1585 }
1586
1587 ConsoleCirBufDelete(cirBufSendCB);
1588 return LOS_OK;
1589 }
1590
1591 #ifdef LOSCFG_KERNEL_SMP
OsWaitConsoleSendTaskPend(UINT32 taskID)1592 VOID OsWaitConsoleSendTaskPend(UINT32 taskID)
1593 {
1594 UINT32 i;
1595 CONSOLE_CB *console = NULL;
1596 LosTaskCB *taskCB = NULL;
1597 INT32 waitTime = 3000; /* 3000: 3 seconds */
1598
1599 for (i = 0; i < CONSOLE_NUM; i++) {
1600 console = g_console[i];
1601 if (console == NULL) {
1602 continue;
1603 }
1604
1605 if (OS_TID_CHECK_INVALID(console->sendTaskID)) {
1606 continue;
1607 }
1608
1609 taskCB = OS_TCB_FROM_TID(console->sendTaskID);
1610 while ((waitTime > 0) && (taskCB->taskEvent == NULL) && (taskID != console->sendTaskID)) {
1611 LOS_Mdelay(1); /* 1: wait console task pend */
1612 --waitTime;
1613 }
1614 }
1615 }
1616
OsWakeConsoleSendTask(VOID)1617 VOID OsWakeConsoleSendTask(VOID)
1618 {
1619 UINT32 i;
1620 CONSOLE_CB *console = NULL;
1621
1622 for (i = 0; i < CONSOLE_NUM; i++) {
1623 console = g_console[i];
1624 if (console == NULL) {
1625 continue;
1626 }
1627
1628 if (console->cirBufSendCB != NULL) {
1629 (VOID)LOS_EventWrite(&console->cirBufSendCB->sendEvent, CONSOLE_CIRBUF_EVENT);
1630 }
1631 }
1632 }
1633 #endif
1634
1635