/* **************************************************************************** * (C) 2005 - Grzegorz Milos - Intel Research Cambridge **************************************************************************** * * File: sched.c * Author: Grzegorz Milos * Changes: Robert Kaiser * * Date: Aug 2005 * * Environment: Xen Minimal OS * Description: simple scheduler for Mini-Os * * The scheduler is non-preemptive (cooperative), and schedules according * to Round Robin algorithm. * **************************************************************************** * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #ifdef SCHED_DEBUG #define DEBUG(_f, _a...) \ printk("MINI_OS(file=sched.c, line=%d) " _f "\n", __LINE__, ## _a) #else #define DEBUG(_f, _a...) ((void)0) #endif struct thread *idle_thread = NULL; MINIOS_LIST_HEAD(exited_threads); static int threads_started; struct thread *main_thread; void inline print_runqueue(void) { struct minios_list_head *it; struct thread *th; minios_list_for_each(it, &idle_thread->thread_list) { th = minios_list_entry(it, struct thread, thread_list); printk(" Thread \"%s\", runnable=%d\n", th->name, is_runnable(th)); } printk("\n"); } void schedule(void) { struct thread *prev, *next, *thread; struct minios_list_head *iterator, *next_iterator; unsigned long flags; prev = current; local_irq_save(flags); if (in_callback) { printk("Must not call schedule() from a callback\n"); BUG(); } if (flags) { printk("Must not call schedule() with IRQs disabled\n"); BUG(); } do { /* Examine all threads. Find a runnable thread, but also wake up expired ones and find the time when the next timeout expires, else use 10 seconds. */ s_time_t now = NOW(); s_time_t min_wakeup_time = now + SECONDS(10); next = NULL; minios_list_for_each_safe(iterator, next_iterator, &idle_thread->thread_list) { thread = minios_list_entry(iterator, struct thread, thread_list); if (!is_runnable(thread) && thread->wakeup_time != 0LL) { if (thread->wakeup_time <= now) wake(thread); else if (thread->wakeup_time < min_wakeup_time) min_wakeup_time = thread->wakeup_time; } if(is_runnable(thread)) { next = thread; /* Put this thread on the end of the list */ minios_list_del(&thread->thread_list); minios_list_add_tail(&thread->thread_list, &idle_thread->thread_list); break; } } if (next) break; /* block until the next timeout expires, or for 10 secs, whichever comes first */ block_domain(min_wakeup_time); /* handle pending events if any */ force_evtchn_callback(); } while(1); local_irq_restore(flags); /* Interrupting the switch is equivalent to having the next thread inturrupted at the return instruction. And therefore at safe point. */ if(prev != next) switch_threads(prev, next); minios_list_for_each_safe(iterator, next_iterator, &exited_threads) { thread = minios_list_entry(iterator, struct thread, thread_list); if(thread != prev) { minios_list_del(&thread->thread_list); free_pages(thread->stack, STACK_SIZE_PAGE_ORDER); xfree(thread); } } } struct thread* create_thread(char *name, void (*function)(void *), void *data) { struct thread *thread; unsigned long flags; /* Call architecture specific setup. */ thread = arch_create_thread(name, function, data); /* Not runable, not exited, not sleeping */ thread->flags = 0; thread->wakeup_time = 0LL; #ifdef HAVE_LIBC _REENT_INIT_PTR((&thread->reent)) #endif set_runnable(thread); local_irq_save(flags); if(idle_thread != NULL) { minios_list_add_tail(&thread->thread_list, &idle_thread->thread_list); } else if(function != idle_thread_fn) { printk("BUG: Not allowed to create thread before initialising scheduler.\n"); BUG(); } local_irq_restore(flags); return thread; } #ifdef HAVE_LIBC static struct _reent callback_reent; struct _reent *__getreent(void) { struct _reent *_reent; if (!threads_started) _reent = _impure_ptr; else if (in_callback) _reent = &callback_reent; else _reent = &get_current()->reent; #ifndef NDEBUG #if defined(__x86_64__) || defined(__x86__) { #ifdef __x86_64__ register unsigned long sp asm ("rsp"); #else register unsigned long sp asm ("esp"); #endif if ((sp & (STACK_SIZE-1)) < STACK_SIZE / 16) { static int overflowing; if (!overflowing) { overflowing = 1; printk("stack overflow\n"); BUG(); } } } #endif #endif return _reent; } #endif void exit_thread(void) { unsigned long flags; struct thread *thread = current; printk("Thread \"%s\" exited.\n", thread->name); local_irq_save(flags); /* Remove from the thread list */ minios_list_del(&thread->thread_list); clear_runnable(thread); /* Put onto exited list */ minios_list_add(&thread->thread_list, &exited_threads); local_irq_restore(flags); /* Schedule will free the resources */ while(1) { schedule(); printk("schedule() returned! Trying again\n"); } } void block(struct thread *thread) { thread->wakeup_time = 0LL; clear_runnable(thread); } void msleep(uint32_t millisecs) { struct thread *thread = get_current(); thread->wakeup_time = NOW() + MILLISECS(millisecs); clear_runnable(thread); schedule(); } void wake(struct thread *thread) { thread->wakeup_time = 0LL; set_runnable(thread); } void idle_thread_fn(void *unused) { threads_started = 1; while (1) { block(current); schedule(); } } DECLARE_MUTEX(mutex); void th_f1(void *data) { struct timeval tv1, tv2; for(;;) { down(&mutex); printk("Thread \"%s\" got semaphore, runnable %d\n", current->name, is_runnable(current)); schedule(); printk("Thread \"%s\" releases the semaphore\n", current->name); up(&mutex); gettimeofday(&tv1, NULL); for(;;) { gettimeofday(&tv2, NULL); if(tv2.tv_sec - tv1.tv_sec > 2) break; } schedule(); } } void th_f2(void *data) { for(;;) { printk("Thread OTHER executing, data 0x%lx\n", data); schedule(); } } void init_sched(void) { printk("Initialising scheduler\n"); #ifdef HAVE_LIBC _REENT_INIT_PTR((&callback_reent)) #endif idle_thread = create_thread("Idle", idle_thread_fn, NULL); MINIOS_INIT_LIST_HEAD(&idle_thread->thread_list); } 7'>137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* **************************************************************************

   This program creates a modified 16bit checksum used for the Netgear
   DGN3500 series routers. The difference between this and a standard
   checksum is that every 0x100 bytes added 0x100 have to be subtracted
   from the sum.

   (C) 2013 Marco Antonio Mauro <marcus90 at gmail.com>

   Based on previous unattributed work.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 ************************************************************************* */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

unsigned char PidDataWW[70] =
{
    0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
    0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
    0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
    0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
} ;

unsigned char PidDataDE[70] =
{
    0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
    0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x37,
    0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
    0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
} ;

unsigned char PidDataNA[70] =
{
    0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
    0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
    0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
    0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
} ;

/* *******************************************************************
   Reads the file into memory and returns pointer to the buffer. */
static char *readfile(char *filename, int *size)
{
	FILE		*fp;
	char		*buffer;
	struct stat	info;

	if (stat(filename,&info)!=0)
		return NULL;

	if ((fp=fopen(filename,"r"))==NULL)
		return NULL;

	buffer=NULL;
	for (;;)
	{
		if ((buffer=(char *)malloc(info.st_size+1))==NULL)
			break;

		if (fread(buffer,1,info.st_size,fp)!=info.st_size)
		{
			free(buffer);
			buffer=NULL;
			break;
		}

		buffer[info.st_size]='\0';
		if(size) *size = info.st_size;

		break;
	}

	(void)fclose(fp);

	return buffer;
}


/* ******************************************************************* */
int main(int argc, char** argv)
{
  unsigned long start, i;
  char *endptr, *buffer, *p;
  int count;  // size of file in bytes
  unsigned short sum, sum1;
  char sumbuf[9];

  if(argc < 3) {
    printf("ERROR: Argument missing!\n\nUsage %s filename starting offset in hex [PID code]\n\n", argv[0]);
    return 1;
  }


  FILE *fp = fopen(argv[1], "a");
  if(!fp) {
    printf("ERROR: File not writeable!\n");
    return 1;
  }
  if(argc = 4)
  {
    printf("%s: PID type: %s\n", argv[0], argv[3]);
    if(strcmp(argv[3], "DE")==0)
      fwrite(PidDataDE, sizeof(PidDataDE), sizeof(char), fp);  /* write DE pid */
    else if(strcmp(argv[3], "NA")==0)
      fwrite(PidDataNA, sizeof(PidDataNA), sizeof(char), fp);  /* write NA pid */
    else /* if(strcmp(argv[3], "WW")) */
      fwrite(PidDataWW, sizeof(PidDataWW), sizeof(char), fp);  /* write WW pid */
  }
  else
    fwrite(PidDataWW, sizeof(PidDataWW), sizeof(char), fp);  /* write WW pid if unspecified */

  fclose(fp);

  /* Read the file to calculate the checksums */
  buffer = readfile(argv[1], &count);
  if(!buffer) {
    printf("ERROR: File %s not found!\n", argv[1]);
    return 1;
  }

  p = buffer;
  for(i = 0; i < count; i++)
  {
	sum += p[i];
  }

  start = strtol(argv[2], &endptr, 16);
  p = buffer+start;
  sum1 = 0;
  for(i = 0; i < count - start; i++)
  {
	sum1 += p[i];
  }

  sprintf(sumbuf,"%04X%04X",sum1,sum);
  /* Append the 2 checksums to end of file */
  fp = fopen(argv[1], "a");
  if(!fp) {
    printf("ERROR: File not writeable!\n");
    return 1;
  }
  fwrite(sumbuf, 8, sizeof(char), fp);
  fclose(fp);
  free(buffer);
  return 0;
}