From c78ef640cd18847a962d3e1fdb84ad6c265b0deb Mon Sep 17 00:00:00 2001 From: Jean-Christophe PLAGNIOL-VILLARD Date: Wed, 10 Aug 2011 12:47:08 +0200 Subject: login: add timeout support If a timeout is specified and expired the command will be executed by default boot Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD Signed-off-by: Sascha Hauer --- common/password.c | 87 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 39 deletions(-) (limited to 'common') diff --git a/common/password.c b/common/password.c index 20e398fc17..ece7704786 100644 --- a/common/password.c +++ b/common/password.c @@ -27,6 +27,7 @@ #include #include #include +#include #if defined(CONFIG_PASSWD_SUM_MD5) #define PASSWD_SUM "md5" @@ -36,56 +37,64 @@ #define PASSWD_SUM "sha256" #endif -int password(unsigned char *passwd, size_t length, int flags) +int password(unsigned char *passwd, size_t length, int flags, int timeout) { unsigned char *buf = passwd; int pos = 0; unsigned char ch; + uint64_t start, second; if (!passwd) return -EINVAL; + start = get_time_ns(); + second = start; + do { - ch = getc(); - - switch (ch) { - case '\r': - case '\n': - *buf = '\0'; - puts("\r\n"); - return pos; - case '\0': - case '\t': - continue; - case CTL_CH('c'): - passwd[0] = '\0'; - puts("\r\n"); - return 0; - case CTL_CH('h'): - case KEY_DEL7: - case KEY_DEL: - if (flags & STAR && pos > 0) - puts("\b \b"); - *buf = '\0'; - buf--; - pos--; - continue; - default: - if (pos < length - 1) { - if (flags & STAR) - putchar('*'); - else if (flags & CLEAR) - putchar(ch); - - *buf = ch; - buf++; - pos++; - } else { - if (flags & STAR) - putchar('\a'); + if (tstc()) { + ch = getc(); + + switch (ch) { + case '\r': + case '\n': + *buf = '\0'; + puts("\r\n"); + return pos; + case '\0': + case '\t': + continue; + case CTL_CH('c'): + passwd[0] = '\0'; + puts("\r\n"); + return 0; + case CTL_CH('h'): + case KEY_DEL7: + case KEY_DEL: + if (flags & STAR && pos > 0) + puts("\b \b"); + *buf = '\0'; + buf--; + pos--; + continue; + default: + if (pos < length - 1) { + if (flags & STAR) + putchar('*'); + else if (flags & CLEAR) + putchar(ch); + + *buf = ch; + buf++; + pos++; + } else { + if (flags & STAR) + putchar('\a'); + } } } - } while(1); + } while (!is_timeout(start, timeout * SECOND) || timeout == 0); + + return -1; } EXPORT_SYMBOL(password); -- cgit v1.2.3